From 753fb1b78bd74077997d84549b4b8fb92112bc74 Mon Sep 17 00:00:00 2001 From: marina Date: Wed, 28 May 2025 10:19:30 +0300 Subject: [PATCH] ERP-417 --- erp24/web/js/validate/validateForm.js | 65 ++++++++++++--------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/erp24/web/js/validate/validateForm.js b/erp24/web/js/validate/validateForm.js index f369f2ad..d59981c0 100755 --- a/erp24/web/js/validate/validateForm.js +++ b/erp24/web/js/validate/validateForm.js @@ -1,42 +1,12 @@ $(".form-validate button").click(function (e) { - var form = $(this).closest('form')[0]; e.preventDefault(); - + var form = $(this).closest('form')[0]; 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) { - errors['__common'] = ['Добавьте хотя бы одно списание перед сохранением.']; - } - - 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; - } - }); - - if (!allRowsHaveImage) { - if (!errors['__common']) errors['__common'] = []; - errors['__common'].push('Пожалуйста, добавьте хотя бы одно фото к каждому списанию перед сохранением.'); - } - - if (Object.keys(errors).length > 0) { - renderErrors(errors); - return false; - } - $.ajax({ type: "post", url: "validate", @@ -47,10 +17,31 @@ $(".form-validate button").click(function (e) { yiiform.find('.has-error').removeClass('has-error'); yiiform.find('.help-block').remove(); - if (data.success) { - yiiform.submit(); + if (!data.success) { + 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; + } + }); + + if (!allRowsHaveImage) { + if (!errors['__common']) errors['__common'] = []; + errors['__common'].push('Пожалуйста, добавьте хотя бы одно фото к каждому списанию перед сохранением.'); + } + + renderErrors(errors); } else { - renderErrors(data.errors); + yiiform.submit(); } }) .fail(function () { @@ -60,10 +51,10 @@ $(".form-validate button").click(function (e) { return false; function renderErrors(errors) { - var values = []; + var messages = []; $.each(errors, function (key, value) { - values.push(value); + messages.push(value); if (key !== '__common') { var field = yiiform.find('[name="' + key + '"]'); @@ -74,7 +65,7 @@ $(".form-validate button").click(function (e) { } }); - var strOut2 = values.flat().join(',
'); + var strOut2 = messages.flat().join(',
'); $('.messages').html(strOut2); } }); \ No newline at end of file -- 2.39.5