Blame view

resources/js/common/showErrors.js 1.34 KB
e77200db5   nologostudio.ru   Initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  export default function (errors, errorDiv, mainDiv) {
      mainDiv = mainDiv || document;
  
      // check if we have any actual errors
      if (errors === undefined || errors === null) {
          return;
      }
  
      // Turn OResult into array of errors.
      if (errors.errorMsg) {
          errors = [errors.errorMsg];
      }
  
      // Turn OResults into array of errors.
      if (errors.OResults) {
          errors = errors.OResults;
      }
  
      // Prepend the main div with the new error div if one does not exist.
      if ($(errorDiv, mainDiv).length < 1) {
          $(mainDiv).prepend('<div id=' + errorDiv + '></div>');
      }
  
      // Reset the error div.
      $(errorDiv, mainDiv).html('<ul></ul>');
      $(errorDiv, mainDiv).addClass('error');
  
      for (var key in errors) {
          if (!errors.hasOwnProperty(key)) {
              continue;
          }
  
          let errorMsg = '';
  
          switch (key) {
              case 'fullName':
                  errorMsg = 'Full Name ' + errors[key].errorMsg;
                  break;
              default:
                  if (typeof errors[key] === 'string') {
                      errorMsg = errors[key];
                  } else {
                      errorMsg = errors[key].errorMsg;
                  }
                  break;
          }
  
          $(errorDiv + ' ul', mainDiv).append('<li>' + errorMsg + '</li>');
          $(errorDiv, mainDiv).show();
      }
  }