From 535033dbd487fba11f9aa20a1732212de5841e32 Mon Sep 17 00:00:00 2001 From: Maks Date: Mon, 10 Nov 2014 16:33:54 +0100 Subject: [PATCH] Core: Reset element styles when using custom highlight methods When use a custom highlight functions which for example place the error class in a different element (like Bootstrap form-group wrapper) this element are not unhighlighted after resetForm. Closes #1323 --- src/core.js | 16 ++++++++++++---- test/test.js | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/core.js b/src/core.js index 40064d1..17b39a7 100644 --- a/src/core.js +++ b/src/core.js @@ -455,10 +455,18 @@ $.extend( $.validator, { this.lastElement = null; this.prepareForm(); this.hideErrors(); - this.elements() - .removeClass( this.settings.errorClass ) - .removeData( "previousValue" ) - .removeAttr( "aria-invalid" ); + var i, elements = this.elements() + .removeData( "previousValue" ) + .removeAttr( "aria-invalid" ); + + if ( this.settings.unhighlight ) { + for ( i = 0; elements[ i ]; i++ ) { + this.settings.unhighlight.call( this, elements[ i ], + this.settings.errorClass, "" ); + } + } else { + elements.removeClass( this.settings.errorClass ); + } }, numberOfInvalids: function() { diff --git a/test/test.js b/test/test.js index 7f759e8..b0efe76 100644 --- a/test/test.js +++ b/test/test.js @@ -810,12 +810,36 @@ test( "resetForm()", function() { var v = $( "#testForm1" ).validate(); v.form(); errors( 2 ); + ok( $( "#firstname" ).hasClass( "error" ) ); $( "#firstname" ).val( "hiy" ); v.resetForm(); errors( 0 ); + ok( !$( "#firstname" ).hasClass( "error" ) ); equal( "", $( "#firstname" ).val(), "form plugin is included, therefor resetForm must also reset inputs, not only errors" ); }); +test( "resetForm() clean styles when custom highlight function is used", function() { + var form = $( "#testForm1clean" ), + e = $( "#firstnamec" ); + form.validate({ + highlight: function( element ) { + $( element ).hide(); + }, + unhighlight: function( element ) { + $( element ).show(); + }, + ignore: "", + errorClass: "invalid", + rules: { + firstnamec: "required" + } + }); + e.valid(); + ok( !e.is( ":visible" ) ); + form.validate().resetForm(); + ok( e.is( ":visible" ) ); +}); + test( "message from title", function() { var v = $( "#withTitle" ).validate(); v.checkForm();