From a6c2a81d7f9ac189d9eb5719c5f88f809637d89c Mon Sep 17 00:00:00 2001 From: Maks3w Date: Wed, 12 Nov 2014 11:17:13 +0100 Subject: [PATCH] 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 --- src/core.js | 5 ++++- test/test.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/core.js b/src/core.js index 17b39a7..0e9b527 100644 --- a/src/core.js +++ b/src/core.js @@ -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; }, diff --git a/test/test.js b/test/test.js index b0efe76..64d838e 100644 --- a/test/test.js +++ b/test/test.js @@ -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(),