Skip to content

Commit 7da2f2f

Browse files
committed
prevent recheck of multiple radio buttons
bug detected when using validation "required" on group of radio buttons with same name attribute, i.e. "payment_method" when not valid (i.e. no radio button was checked), an error message span would be created for each radio button element. this patch adds a memory array named checkedInputs[] to hold the names of elements that have been checked during the iteration of isValid() func, thereby preventing a recheck of elements with the same name, in the situation of radio buttons. during the iteration, each element is added to this array. if the element name already exists in the array, the validation of that element will not be performed. since each form element has unique names, except for radio buttons and checkbox groups, this does not affect other input types. Important Notes on Multiple Checkboxes: when using validation on a group of checkboxes, set validation rule to checkboxes_group, and specify qty attribute, i.e. min1 otherwise the keyup event will trigger the validation on the checkbox, without being able to read the other checkboxes in same named group, and you will see an error message, even though the required qty of checkboxes is checked
1 parent 1b99b52 commit 7da2f2f

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

form-validator/jquery.form-validator.js

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
* @return {jQuery}
199199
*/
200200
$.fn.validateInputOnBlur = function(language, conf, attachKeyupEvent, eventType) {
201-
console.log(eventType);
201+
202202
$.formUtils.eventType = eventType;
203203

204204
if( (this.valAttr('suggestion-nr') || this.valAttr('postpone') || this.hasClass('hasDatepicker')) && !window.postponedValidation ) {
@@ -275,7 +275,7 @@
275275
};
276276

277277
/**
278-
* Function that validate all inputs in given element
278+
* Function that validates all inputs in active form
279279
*
280280
* @param {Object} [language]
281281
* @param {Object} [conf]
@@ -317,6 +317,9 @@
317317
}
318318
},
319319

320+
/** HoldsInputs already validated, to prevent recheck of mulitple checkboxes & radios */
321+
checkedInputs = [],
322+
320323
/** Error messages for this validation */
321324
errorMessages = [],
322325

@@ -350,30 +353,38 @@
350353
$form.find('input,textarea,select').filter(':not([type="submit"],[type="button"])').each(function() {
351354
var $elem = $(this);
352355
var elementType = $elem.attr('type');
353-
if (!ignoreInput($elem.attr('name'), elementType)) {
354-
355-
var validation = $.formUtils.validateInput(
356-
$elem,
357-
language,
358-
conf,
359-
$form,
360-
'submit'
361-
);
362-
363-
// Run element validation callback
364-
if( typeof conf.onElementValidate == 'function' ) {
365-
conf.onElementValidate((validation === true), $elem, $form, validation);
366-
}
367-
368-
if(validation !== true) {
369-
addErrorMessage(validation, $elem);
370-
} else {
371-
$elem
372-
.valAttr('current-error', false)
373-
.addClass('valid')
374-
.parent()
375-
.addClass('has-success');
376-
}
356+
var elementName = $elem.attr('name');
357+
if (!ignoreInput(elementName, elementType)) {
358+
359+
// do not recheck multiple elements with same name, i.e. checkboxes, radios
360+
if ($.inArray(elementName, checkedInputs) < 0 ) {
361+
checkedInputs.push(elementName);
362+
363+
var validation = $.formUtils.validateInput(
364+
$elem,
365+
language,
366+
conf,
367+
$form,
368+
'submit'
369+
);
370+
371+
// Run element validation callback
372+
if( typeof conf.onElementValidate == 'function' ) {
373+
conf.onElementValidate((validation === true), $elem, $form, validation);
374+
}
375+
376+
if(validation !== true) {
377+
addErrorMessage(validation, $elem);
378+
} else {
379+
$elem
380+
.valAttr('current-error', false)
381+
.addClass('valid')
382+
.parent()
383+
.addClass('has-success');
384+
}
385+
386+
}
387+
377388
}
378389

379390
});
@@ -859,12 +870,13 @@
859870
var validator = $.formUtils.validators[rule];
860871

861872
if( validator && typeof validator['validatorFunction'] == 'function' ) {
873+
862874
// special change of element for checkbox_group rule
863875
if ( rule == 'validate_checkbox_group' ) {
864-
// set element to first in group, so error msg is set only once
876+
// set element to first in group, so error msg attr doesn't need to be set on all elements in group
865877
$elem = $("[name='"+$elem.attr('name')+"']:eq(0)");
866878
}
867-
879+
868880
var isValid = null;
869881
if( eventContext != 'keyup' || validator.validateOnKeyUp ) {
870882
isValid = validator.validatorFunction(value, $elem, conf, language, $form);

0 commit comments

Comments
 (0)