]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
ERP-417
authormarina <m.zozirova@gmail.com>
Wed, 28 May 2025 07:18:10 +0000 (10:18 +0300)
committermarina <m.zozirova@gmail.com>
Wed, 28 May 2025 07:18:10 +0000 (10:18 +0300)
erp24/web/js/validate/validateForm.js

index 48780fbd2e3a9b98ff8f49d9f8b46769ac382b6d..f369f2adf9f76c90442871808264ad2e13afccfd 100755 (executable)
@@ -3,10 +3,14 @@ $(".form-validate button").click(function (e) {
     e.preventDefault();
 
     var yiiform = $("#" + form.id);
+    var errors = {};
+
+    $('.messages').html('');
+    yiiform.find('.has-error').removeClass('has-error');
+    yiiform.find('.help-block').remove();
 
     if ($('.multiple-input-list__item').length === 0) {
-        alert('Добавьте хотя бы одно списание перед сохранением.');
-        return false;
+        errors['__common'] = ['Добавьте хотя бы одно списание перед сохранением.'];
     }
 
     let allRowsHaveImage = true;
@@ -24,7 +28,12 @@ $(".form-validate button").click(function (e) {
     });
 
     if (!allRowsHaveImage) {
-        alert('Пожалуйста, добавьте хотя бы одно фото к каждому списанию перед сохранением.');
+        if (!errors['__common']) errors['__common'] = [];
+        errors['__common'].push('Пожалуйста, добавьте хотя бы одно фото к каждому списанию перед сохранением.');
+    }
+
+    if (Object.keys(errors).length > 0) {
+        renderErrors(errors);
         return false;
     }
 
@@ -41,23 +50,7 @@ $(".form-validate button").click(function (e) {
             if (data.success) {
                 yiiform.submit();
             } else {
-                if (data.errors) {
-                    var values = [];
-
-                    $.each(data.errors, function (key, value) {
-                        values.push(value);
-                        var field = yiiform.find('[name="' + key + '"]');
-                        if (field.length) {
-                            field.closest('.form-group').addClass('has-error');
-                            field.after('<div class="help-block">' + value[0] + '</div>');
-                        }
-                    });
-
-                    var strOut = values.join(', \n');
-                    var strOut2 = values.join(', <br>');
-                    alert(strOut);
-                    $('.messages').html(strOut2);
-                }
+                renderErrors(data.errors);
             }
         })
         .fail(function () {
@@ -65,4 +58,23 @@ $(".form-validate button").click(function (e) {
         });
 
     return false;
-});
+
+    function renderErrors(errors) {
+        var values = [];
+
+        $.each(errors, function (key, value) {
+            values.push(value);
+
+            if (key !== '__common') {
+                var field = yiiform.find('[name="' + key + '"]');
+                if (field.length) {
+                    field.closest('.form-group').addClass('has-error');
+                    field.after('<div class="help-block">' + value[0] + '</div>');
+                }
+            }
+        });
+
+        var strOut2 = values.flat().join(', <br>');
+        $('.messages').html(strOut2);
+    }
+});
\ No newline at end of file