Skip to content

Commit

Permalink
Merge pull request #122 from legalthings/required_checkbox_does_not_w…
Browse files Browse the repository at this point in the history
…ork_for_option_groups

Fix validation of group fields
  • Loading branch information
svenstm committed Dec 6, 2018
2 parents ee774b2 + b7ec94a commit 2c72f5c
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions js/legalform-validation.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit 2c72f5c

Please sign in to comment.