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

index d59981c054397052f1ec05b94365be85bf74658b..55ad42d76f6075ff52cde3679f723fe86c1acc63 100755 (executable)
@@ -17,30 +17,35 @@ $(".form-validate button").click(function (e) {
             yiiform.find('.has-error').removeClass('has-error');
             yiiform.find('.help-block').remove();
 
-            if (!data.success) {
-                let errors = data.errors || {};
+            let errors = data.errors || {};
 
-                // Проверка на фото после AJAX
-                let allRowsHaveImage = true;
-                $('.multiple-input-list__item').each(function () {
-                    let row = $(this);
-                    let hasImageInRow = row.find('.list-cell__images_row img').length > 0;
-                    let fileInput = row.find('.list-cell__imageFiles input[type="file"]');
-                    let hasFilesSelected = fileInput.length && fileInput[0].files.length > 0;
-
-                    if (!hasImageInRow && !hasFilesSelected) {
-                        allRowsHaveImage = false;
-                        return false;
-                    }
-                });
+            const rows = $('.multiple-input-list__item');
+            if (rows.length === 0) {
+                if (!errors['__common']) errors['__common'] = [];
+                errors['__common'].push('Добавьте хотя бы одно списание.');
+            }
 
-                if (!allRowsHaveImage) {
-                    if (!errors['__common']) errors['__common'] = [];
-                    errors['__common'].push('Пожалуйста, добавьте хотя бы одно фото к каждому списанию перед сохранением.');
+            let allRowsHaveImage = true;
+            rows.each(function () {
+                let row = $(this);
+                let hasImageInRow = row.find('.list-cell__images_row img').length > 0;
+                let fileInput = row.find('.list-cell__imageFiles input[type="file"]');
+                let hasFilesSelected = fileInput.length && fileInput[0].files.length > 0;
+                if (!hasImageInRow && !hasFilesSelected) {
+                    allRowsHaveImage = false;
+                    return false;
                 }
+            });
+
+            if (!allRowsHaveImage) {
+                if (!errors['__common']) errors['__common'] = [];
+                errors['__common'].push('Пожалуйста, добавьте хотя бы одно фото к каждому списанию перед сохранением.');
+            }
 
+            if (Object.keys(errors).length > 0) {
                 renderErrors(errors);
             } else {
+                yiiform.off('submit');
                 yiiform.submit();
             }
         })
@@ -52,10 +57,8 @@ $(".form-validate button").click(function (e) {
 
     function renderErrors(errors) {
         var messages = [];
-
         $.each(errors, function (key, value) {
             messages.push(value);
-
             if (key !== '__common') {
                 var field = yiiform.find('[name="' + key + '"]');
                 if (field.length) {
@@ -64,8 +67,7 @@ $(".form-validate button").click(function (e) {
                 }
             }
         });
-
         var strOut2 = messages.flat().join(', <br>');
         $('.messages').html(strOut2);
     }
-});
\ No newline at end of file
+});