Fixed #177 - Fix validation of a single radio or checkbox input

The validator element() method delegates to check(). When check() is
passed a radio or checkbox input element (call it 'A'), it instead
checks the first element with the same name in the form (call it 'B').

If element B is judged invalid, a bug occurs. Element A will have been
added to the currentElements array, but element B is what is added to
the errorList. So, when showErrors is called, element A will be in
validElements(), and will be unhighlighted.

This is fixed by having element() also change its target to be the
first element of the same name when passed a checkbox or radio input.

Signed-off-by: Eric Naeseth <eric@thumbtack.com>
This commit is contained in:
Eric Naeseth
2011-08-17 14:54:46 -07:00
committed by Jörn Zaefferer
parent 3f187a5d01
commit f2321e1f9f
3 changed files with 33 additions and 7 deletions

17
jquery.validate.js vendored
View File

@@ -349,7 +349,7 @@ $.extend($.validator, {
// http://docs.jquery.com/Plugins/Validation/Validator/element
element: function( element ) {
element = this.clean( element );
element = this.validationTargetFor( this.clean( element ) );
this.lastElement = element;
this.prepareElement( element );
this.currentElements = $(element);
@@ -493,12 +493,7 @@ $.extend($.validator, {
},
check: function( element ) {
element = this.clean( element );
// if radio/checkbox, validate first element in group instead
if (this.checkable(element)) {
element = this.findByName( element.name ).not(this.settings.ignore)[0];
}
element = this.validationTargetFor( this.clean( element ) );
var rules = $(element).rules();
var dependencyMismatch = false;
@@ -679,6 +674,14 @@ $.extend($.validator, {
return this.groups[element.name] || (this.checkable(element) ? element.name : element.id || element.name);
},
validationTargetFor: function(element) {
// if radio/checkbox, validate first element in group instead
if (this.checkable(element)) {
element = this.findByName( element.name ).not(this.settings.ignore)[0];
}
return element;
},
checkable: function( element ) {
return /radio|checkbox/i.test(element.type);
},