Skip to content

Commit a6c2a81

Browse files
Maks3wjzaefferer
authored andcommitted
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 jquery-validation#1327
1 parent d80c469 commit a6c2a81

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/core.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,19 @@ $.extend($.fn, {
9090
},
9191
// http://jqueryvalidation.org/valid/
9292
valid: function() {
93-
var valid, validator;
93+
var valid, validator, errorList;
9494

9595
if ( $( this[ 0 ] ).is( "form" ) ) {
9696
valid = this.validate().form();
9797
} else {
98+
errorList = [];
9899
valid = true;
99100
validator = $( this[ 0 ].form ).validate();
100101
this.each( function() {
101102
valid = validator.element( this ) && valid;
103+
errorList = errorList.concat( validator.errorList );
102104
});
105+
validator.errorList = errorList;
103106
}
104107
return valid;
105108
},

test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,21 @@ test( "focusInvalid()", function() {
719719
v.focusInvalid();
720720
});
721721

722+
test( "focusInvalid() after validate a custom set of inputs", function() {
723+
var form = $( "#testForm1" ),
724+
validator = form.validate(),
725+
// It's important the order of Valid, Invalid, Valid so last active element it's a valid element before focus
726+
inputs = $( "#firstname, #lastname, #something" );
727+
728+
$( "#firstname" ).val( "ok" );
729+
730+
ok( !inputs.valid(), "just one invalid");
731+
732+
validator.focusInvalid();
733+
734+
equal( form[ 0 ].ownerDocument.activeElement, $( "#lastname" )[0], "focused first element" );
735+
});
736+
722737
test( "findLastActive()", function() {
723738
expect( 3 );
724739
var v = $( "#testForm1" ).validate(),

0 commit comments

Comments
 (0)