Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Commit

Permalink
Merge branch 'bug-fix-field-validation' into 'master'
Browse files Browse the repository at this point in the history
Fix checkbox validator

See merge request !40
mike-koch committed Jan 7, 2017
2 parents 24e2607 + 0215657 commit 6924ae0
Showing 3 changed files with 35 additions and 26 deletions.
14 changes: 10 additions & 4 deletions admin/new_ticket.php
Original file line number Diff line number Diff line change
@@ -388,7 +388,6 @@ class="important">*</span></label>

/* Select drop-down box */
case 'select':

$cls = in_array($k, $_SESSION['iserror']) ? ' isError' : '';

echo '<div class="form-group' . $cls . '"><label for="' . $v['name'] . '" class="col-sm-3 control-label">' . $v['name'] . ' ' . $v['req'] . '</label>
@@ -416,8 +415,11 @@ class="important">*</span></label>

/* Checkbox */
case 'checkbox':

$cls = in_array($k, $_SESSION['iserror']) ? ' isError' : '';

$validator = $v['req'] == '<span class="important">*</span>' ? 'data-checkbox="' . $k . '"' : '';
$required_attribute = $validator == '' ? '' : ' data-error="' . $hesklang['this_field_is_required'] . '"';

echo '<div class="form-group' . $cls . '"><label class="col-sm-3 control-label">' . $v['name'] . ' ' . $v['req'] . '</label><div align="left" class="col-sm-9">';

foreach ($v['value']['checkbox_options'] as $option) {
@@ -427,7 +429,7 @@ class="important">*</span></label>
$checked = '';
}

echo '<div class="checkbox"><label><input type="checkbox" name="' . $k . '[]" value="' . $option . '" ' . $checked . $required_attribute . '> ' . $option . '</label></div>';
echo '<div class="checkbox"><label><input ' . $validator . ' type="checkbox" name="' . $k . '[]" value="' . $option . '" ' . $checked . $required_attribute . '> ' . $option . '</label></div>';
}
echo '
<div class="help-block with-errors"></div></div></div>';
@@ -783,6 +785,10 @@ function setMessage(msgid) {
/* Checkbox */
case 'checkbox':
$cls = in_array($k, $_SESSION['iserror']) ? ' isError' : '';

$validator = $v['req'] == '<span class="important">*</span>' ? 'data-checkbox="' . $k . '"' : '';
$required_attribute = $validator == '' ? '' : ' data-error="' . $hesklang['this_field_is_required'] . '"';

echo '<div class="form-group' . $cls . '"><label class="col-sm-3 control-label">' . $v['name'].' '.$v['req'] . '</label><div align="left" class="col-sm-9">';

foreach ($v['value']['checkbox_options'] as $option) {
@@ -792,7 +798,7 @@ function setMessage(msgid) {
$checked = '';
}

echo '<div class="checkbox"><label><input type="checkbox" name="' . $k . '[]" value="' . $option . '" ' . $checked . ' ' . $required_attribute . '> ' . $option . '</label></div>';
echo '<div class="checkbox"><label><input ' . $validator . ' type="checkbox" name="' . $k . '[]" value="' . $option . '" ' . $checked . $required_attribute .'> ' . $option . '</label></div>';
}
echo '<div class="help-block with-errors"></div></div></div>';
break;
8 changes: 6 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
@@ -514,6 +514,8 @@ class="important">*</span></label>
/* Checkbox */
case 'checkbox':
$cls = in_array($k,$_SESSION['iserror']) ? 'isError' : '';
$validator = $v['req'] == '<span class="important">*</span>' ? 'data-checkbox="' . $k . '"' : '';
$required_attribute = $validator == '' ? '' : ' data-error="' . $hesklang['this_field_is_required'] . '"';
echo '
<div class="form-group '.$cls.'">
<label for="'.$k.'" class="col-sm-3 control-label">'.$v['name'].' '.$v['req'].'</label>
@@ -530,7 +532,7 @@ class="important">*</span></label>
$checked = '';
}

echo '<div class="checkbox"><label><input type="checkbox" name="'.$k.'[]" value="'.$option.'" '.$checked.' '.$required_attribute.'> '.$option.'</label></div>';
echo '<div class="checkbox"><label><input ' . $validator . ' type="checkbox" name="'.$k.'[]" value="'.$option.'" '.$checked.' ' . $required_attribute . '> '.$option.'</label></div>';
}
echo '
<div class="help-block with-errors"></div>
@@ -840,6 +842,8 @@ class="important">*</span></label>
/* Checkbox */
case 'checkbox':
$cls = in_array($k,$_SESSION['iserror']) ? 'isError' : '';
$validator = $v['req'] == '<span class="important">*</span>' ? 'data-checkbox="' . $k . '"' : '';
$required_attribute = $validator == '' ? '' : ' data-error="' . $hesklang['this_field_is_required'] . '"';
echo '
<div class="form-group '.$cls.'">
<label for="'.$k.'" class="col-sm-3 control-label">'.$v['name'].' '.$v['req'].'</label>
@@ -856,7 +860,7 @@ class="important">*</span></label>
$checked = '';
}

echo '<div class="checkbox"><label><input type="checkbox" name="'.$k.'[]" value="'.$option.'" '.$checked.' '.$required_attribute.'> '.$option.'</label></div>';
echo '<div class="checkbox"><label><input ' . $validator . ' type="checkbox" name="'.$k.'[]" value="'.$option.'" '.$checked.' '.$required_attribute.'> '.$option.'</label></div>';
}
echo '
<div class="help-block with-errors"></div>
39 changes: 19 additions & 20 deletions js/validation-scripts.js
Original file line number Diff line number Diff line change
@@ -16,43 +16,42 @@ 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');
});
}

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');
});
}

0 comments on commit 6924ae0

Please sign in to comment.