Skip to content

Commit

Permalink
Core: fix remote validation when input is the same as in aborted requ…
Browse files Browse the repository at this point in the history
…est (#2481)

Fixes #2479
  • Loading branch information
bidord committed Apr 21, 2024
1 parent 0c811ce commit 569e622
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/core.js
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
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
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

0 comments on commit 569e622

Please sign in to comment.