8000 Core: Bind the `blur` event just once in `equalTo` rule by Arkni · Pull Request #1709 · jquery-validation/jquery-validation · GitHub
[go: up one dir, main page]

Skip to content

Core: Bind the blur event just once in equalTo rule #1709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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