Core: Focus invalid element when validating a custom set of inputs

Invalid element is not focused when validate a custom set of inputs and
the last one is a valid input. The issue is element method, via
prepareElement method, via reset method, resets errorList state after
validate each input so the global state of the validator is not
preserved.

Closes #1327
This commit is contained in:
Maks3w
2014-11-12 11:17:13 +01:00
committed by Jörn Zaefferer
parent d80c469a31
commit a6c2a81d7f
2 changed files with 19 additions and 1 deletions

View File

@@ -90,16 +90,19 @@ $.extend($.fn, {
},
// http://jqueryvalidation.org/valid/
valid: function() {
var valid, validator;
var valid, validator, errorList;
if ( $( this[ 0 ] ).is( "form" ) ) {
valid = this.validate().form();
} else {
errorList = [];
valid = true;
validator = $( this[ 0 ].form ).validate();
this.each( function() {
valid = validator.element( this ) && valid;
errorList = errorList.concat( validator.errorList );
});
validator.errorList = errorList;
}
return valid;
},

View File

@@ -719,6 +719,21 @@ test( "focusInvalid()", function() {
v.focusInvalid();
});
test( "focusInvalid() after validate a custom set of inputs", function() {
var form = $( "#testForm1" ),
validator = form.validate(),
// It's important the order of Valid, Invalid, Valid so last active element it's a valid element before focus
inputs = $( "#firstname, #lastname, #something" );
$( "#firstname" ).val( "ok" );
ok( !inputs.valid(), "just one invalid");
validator.focusInvalid();
equal( form[ 0 ].ownerDocument.activeElement, $( "#lastname" )[0], "focused first element" );
});
test( "findLastActive()", function() {
expect( 3 );
var v = $( "#testForm1" ).validate(),