From 02156576503294991b4362a188ebbe9478eacdf3 Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Fri, 6 Jan 2017 21:55:14 -0500 Subject: [PATCH] Fix checkbox validator --- admin/new_ticket.php | 14 ++++++++++---- index.php | 8 ++++++-- js/validation-scripts.js | 39 +++++++++++++++++++-------------------- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/admin/new_ticket.php b/admin/new_ticket.php index 75e6bbe3..00387f38 100644 --- a/admin/new_ticket.php +++ b/admin/new_ticket.php @@ -388,7 +388,6 @@ $show_quick_help = $show['show']; /* Select drop-down box */ case 'select': - $cls = in_array($k, $_SESSION['iserror']) ? ' isError' : ''; echo '
@@ -416,8 +415,11 @@ $show_quick_help = $show['show']; /* Checkbox */ case 'checkbox': - $cls = in_array($k, $_SESSION['iserror']) ? ' isError' : ''; + + $validator = $v['req'] == '*' ? 'data-checkbox="' . $k . '"' : ''; + $required_attribute = $validator == '' ? '' : ' data-error="' . $hesklang['this_field_is_required'] . '"'; + echo '
'; foreach ($v['value']['checkbox_options'] as $option) { @@ -427,7 +429,7 @@ $show_quick_help = $show['show']; $checked = ''; } - echo '
'; + echo '
'; } echo '
'; @@ -783,6 +785,10 @@ $show_quick_help = $show['show']; /* Checkbox */ case 'checkbox': $cls = in_array($k, $_SESSION['iserror']) ? ' isError' : ''; + + $validator = $v['req'] == '*' ? 'data-checkbox="' . $k . '"' : ''; + $required_attribute = $validator == '' ? '' : ' data-error="' . $hesklang['this_field_is_required'] . '"'; + echo '
'; foreach ($v['value']['checkbox_options'] as $option) { @@ -792,7 +798,7 @@ $show_quick_help = $show['show']; $checked = ''; } - echo '
'; + echo '
'; } echo '
'; break; diff --git a/index.php b/index.php index c9aecaec..caa38350 100644 --- a/index.php +++ b/index.php @@ -514,6 +514,8 @@ function print_add_ticket() /* Checkbox */ case 'checkbox': $cls = in_array($k,$_SESSION['iserror']) ? 'isError' : ''; + $validator = $v['req'] == '*' ? 'data-checkbox="' . $k . '"' : ''; + $required_attribute = $validator == '' ? '' : ' data-error="' . $hesklang['this_field_is_required'] . '"'; echo '
@@ -530,7 +532,7 @@ function print_add_ticket() $checked = ''; } - echo '
'; + echo '
'; } echo '
@@ -840,6 +842,8 @@ function print_add_ticket() /* Checkbox */ case 'checkbox': $cls = in_array($k,$_SESSION['iserror']) ? 'isError' : ''; + $validator = $v['req'] == '*' ? 'data-checkbox="' . $k . '"' : ''; + $required_attribute = $validator == '' ? '' : ' data-error="' . $hesklang['this_field_is_required'] . '"'; echo '
@@ -856,7 +860,7 @@ function print_add_ticket() $checked = ''; } - echo '
'; + echo '
'; } echo '
diff --git a/js/validation-scripts.js b/js/validation-scripts.js index 1b758b75..2d5ba8e1 100644 --- a/js/validation-scripts.js +++ b/js/validation-scripts.js @@ -16,24 +16,21 @@ function buildValidatorForTicketSubmission(formName, validationText) { $('form[name="' + formName + '"]').validator({ custom: { checkbox: function($el) { - var checkboxes = $('input[name="' + $el.attr('data-checkbox') + '[]"]'); + var name = $el.data('checkbox'); + var $checkboxes = $el.closest('form').find('input[name="' + name + '[]"]'); - for (var checkbox in checkboxes) { - if (checkboxes[checkbox].checked) { - return true; - } - } - return false; - }, - multiselect: function($el) { - var count = $('select[name="' + $el.attr('data-multiselect') + '[]"] :selected').length; - return count > 0; + return $checkboxes.is(':checked'); } }, errors: { - checkbox: validationText, - multiselect: validationText + checkbox: validationText } + }).on('change.bs.validator', '[data-checkbox]', function (e) { + var $el = $(e.target); + var name = $el.data('checkbox'); + var $checkboxes = $el.closest('form').find('input[name="' + name + '[]"]'); + + $checkboxes.not(':checked').trigger('input'); }); } @@ -41,18 +38,20 @@ function buildValidatorForPermissionTemplates(formId, validationText) { $('#' + formId).validator({ custom: { checkbox: function($el) { - var checkboxes = $('input[data-modal="new-' + $el.attr('data-checkbox') + '"]'); + var name = $el.data('checkbox'); + var $checkboxes = $el.closest('form').find('input[data-modal="new-' + name + '"]'); - for (var checkbox in checkboxes) { - if (checkboxes[checkbox].checked) { - return true; - } - } - return false; + return $checkboxes.is(':checked'); } }, errors: { checkbox: validationText } + }).on('change.bs.validator', '[data-modal]', function (e) { + var $el = $(e.target); + var name = $el.data('checkbox'); + var $checkboxes = $el.closest('form').find('input[data-modal="new-' + name + '"]'); + + $checkboxes.not(':checked').trigger('input'); }); } \ No newline at end of file