Skip to content

Core: fix remote validation when input is the same as in aborted request #2481

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
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
3 changes: 2 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1594,11 +1594,12 @@ $.extend( $.validator, {

param = typeof param === "string" && { url: param } || param;
optionDataString = $.param( $.extend( { data: value }, param.data ) );
if ( previous.old === optionDataString ) {
if ( previous.valid !== null && previous.old === optionDataString ) {
return previous.valid;
}

previous.old = optionDataString;
previous.valid = null;
validator = this;
this.startRequest( element );
data = {};
Expand Down
38 changes: 37 additions & 1 deletion test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ QUnit.test( "remote, data previous querystring", function( assert ) {
rules: {
lastname: {
remote: {
url: "users.php",
url: "users3.php",
type: "POST",
data: {
firstname: function() {
Expand Down Expand Up @@ -839,6 +839,42 @@ QUnit.test( "Fix #2434: race condition in remote validation rules", function( as
} );
} );

QUnit.test( "Fix #2479: Remote validation fails when input is the same as in aborted request", function( assert ) {
var e = $( "#username" ),
done1 = assert.async(),
v = $( "#userForm" ).validate( {
rules: {
username: {
remote: {
url: "users.php"
}
}
},
messages: {
username: {
remote: $.validator.format( "{0} in use" )
}
}
} );

e.val( "Peter" );
v.element( e );
assert.equal( e.hasClass( "error" ), false, "Field 'username' should not have the error class" );
assert.equal( e.hasClass( "pending" ), true, "field 'username' should have the pending class" );

e.val( "Peter" );
v.element( e );
assert.equal( e.hasClass( "error" ), false, "Field 'username' should not have the error class" );
assert.equal( e.hasClass( "pending" ), true, "field 'username' should have the pending class" );

setTimeout( function() {
assert.equal( v.errorList[ 0 ].message, "Peter in use" );
assert.equal( e.hasClass( "error" ), true, "Field 'username' should have the error class" );
assert.equal( e.hasClass( "pending" ), false, "field 'username' should not have the pending class" );
done1();
} );
} );

QUnit.module( "additional methods" );

QUnit.test( "phone (us)", function( assert ) {
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ $.mockjax( {
responseTime: 1
} );

$.mockjax( {
url: "users3.php",
data: {
lastname: "last-name"
},
responseText: "false",
responseStatus: 200,
responseTime: 1
} );

$.mockjax( {
url: "echo.php",
response: function( data ) {
Expand Down