From ac091cb58ee0a5d27e3e745c021f5fcb343c74b2 Mon Sep 17 00:00:00 2001 From: Sergey Boltonosov Date: Sat, 24 Nov 2018 00:07:50 +0700 Subject: [PATCH] Fix validation of group fields --- js/legalform-validation.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/js/legalform-validation.js b/js/legalform-validation.js index 3d30acb..a9005fd 100644 --- a/js/legalform-validation.js +++ b/js/legalform-validation.js @@ -2,10 +2,11 @@ * Validation for LegalForm */ (function($) { - function LegalFormValidation() { + function LegalFormValidation(isTestEnv) { this.ractive = null; this.el = null; this.elWizard = null; + this.isTestEnv = !!isTestEnv; //Fields for custom validation var textFields = 'input[type="text"], input[type="number"], input[type="email"], textarea'; @@ -228,25 +229,26 @@ } // Implement validation for group checkboxes - if (meta.type === 'group') { + if (meta.type === 'group' && $input.attr('multiple')) { const checkBoxId = $input.attr('data-id'); - const allCheckboxes = $("[data-id='" + checkBoxId + "']"); + const $allCheckboxes = $('[data-id="' + checkBoxId + '"]'); const isRequired = !$input.closest('.form-group').find('label > span').length ? false : $input.closest('.form-group').find('label > span')[0].className === 'required' ? true : false; - let checked = 0; - - for (var i = 0; i < allCheckboxes.length; i++) { - if (allCheckboxes[i].checked) { - checked++; - } else if (allCheckboxes[i].type !== 'radio') { - $(allCheckboxes[i]).prop('required', false); + if (isRequired && this.isTestEnv) { + $allCheckboxes.prop('required', false); + } else { + let checked = 0; + for (var i = 0; i < $allCheckboxes.length; i++) { + if ($allCheckboxes[i].checked) checked++; } - } - if (isRequired && checked === 0) { - $input.get(0).setCustomValidity(error); - return; + if (isRequired) $allCheckboxes.prop('required', !checked); + + if (isRequired && checked === 0) { + $input.get(0).setCustomValidity(error); + return; + } } }