Skip to content

Commit

Permalink
Core: Bind the blur event just once in equalTo rule
Browse files Browse the repository at this point in the history
bind the event just once, avoiding the unbind-rebind overhead.
Also, unbind it when destroying the plugin.

Ref #1704
Ref #1707
  • Loading branch information
Arkni committed Feb 12, 2016
1 parent 5545dbc commit ef22360
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,10 @@ $.extend( $.validator, {

$( this.currentForm )
.off( ".validate" )
.removeData( "validator" );
.removeData( "validator" )
.find( ".validate-equalTo-blur" )
.off( ".validate-equalTo" )
.removeClass( "validate-equalTo-blur" );
}

},
Expand Down Expand Up @@ -1386,10 +1389,9 @@ $.extend( $.validator, {
equalTo: function( value, element, param ) {

// Bind to the blur event of the target in order to revalidate whenever the target field is updated
// TODO find a way to bind the event just once, avoiding the unbind-rebind overhead
var target = $( param );
if ( this.settings.onfocusout ) {
target.off( ".validate-equalTo" ).on( "blur.validate-equalTo", function() {
if ( this.settings.onfocusout && target.not( ".validate-equalTo-blur" ).length ) {
target.addClass( "validate-equalTo-blur" ).on( "blur.validate-equalTo", function() {
$( element ).valid();
} );
}
Expand Down
18 changes: 12 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2150,15 +2150,21 @@ test( "Validation triggered on radio and checkbox via click", function() {
} );

test( "destroy()", function() {
expect( 2 );
expect( 6 );

var form = $( "#testForm5" ),
validate = form.validate();

var form = $( "#form" ),
validate = form.validate();
strictEqual( form.data( "validator" ), validate );

strictEqual( $( form ).data( "validator" ), validate );
form.valid();
equal( $( "#x1", form ).hasClass( "validate-equalTo-blur" ), true, "The blur event should be bound to this element" );
equal( $( "#x2", form ).hasClass( "validate-equalTo-blur" ), true, "The blur event should be bound to this element" );

validate.destroy();
strictEqual( $( form ).data( "validator" ), undefined );
validate.destroy();
strictEqual( form.data( "validator" ), undefined );
equal( $( "#x1", form ).hasClass( "validate-equalTo-blur" ), false, "The blur event should be unbound from this element" );
equal( $( "#x2", form ).hasClass( "validate-equalTo-blur" ), false, "The blur event should be unbound from this element" );
} );

test( "#1618: Errorlist containing more errors than it should", function() {
Expand Down

0 comments on commit ef22360

Please sign in to comment.