Skip to content

Commit

Permalink
Core: core.js, methods.js, Fix for #1567 - jquery-validation ignores …
Browse files Browse the repository at this point in the history
…data obj when validating, incorrectly returning previous status

This change determines if either the validated element's value OR one of the data properties has changed before aborting the remote request. If your remote validation is skipped due to being dependent on the value of another field, this fixes that issue.
  • Loading branch information
christopherbauer committed Sep 11, 2015
1 parent 6129ab6 commit 17a56ba
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ $.extend( $.validator, {
}

var previous = this.previousValue( element ),
validator, data;
validator, data, optionDataString;

if (!this.settings.messages[ element.name ] ) {
this.settings.messages[ element.name ] = {};
Expand All @@ -1300,12 +1300,12 @@ $.extend( $.validator, {
this.settings.messages[ element.name ].remote = previous.message;

param = typeof param === "string" && { url: param } || param;

if ( previous.old === value ) {
optionDataString = $.param( $.extend( { data: value }, param.data ) );
if (previous.old === optionDataString) {
return previous.valid;
}

previous.old = value;
previous.old = optionDataString;
validator = this;
this.startRequest( element );
data = {};
Expand Down
36 changes: 36 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,42 @@ asyncTest("remote extensions", function() {
strictEqual( v.element(e), true, "still invalid, because remote validation must block until it returns; dependency-mismatch considered as valid though" );
});

test("remote, data previous querystring", function() {
expect(4);
var change = true,
e = $("#username"),
v = $("#userForm").validate({
rules: {
username: {
required: true,
remote: {
url: "users.php",
type: "POST",
data: {
email: function() {
if ( change ) {
change = false;
return "email.com";
}
return "email2.com";
}
},
beforeSend: function(request, settings) {
if ( change ) {
deepEqual(settings.data, "username=asdf&email=email.com");
} else {
deepEqual(settings.data, "username=asdf&email=email2.com");
}
}
}
}
}
});
$("#username").val("asdf");
strictEqual( v.element(e), true, "new email value (email.com), new request; dependency-mismatch considered as valid though" );
strictEqual( v.element(e), true, "new email value (email2.com), new request; dependency-mismatch considered as valid though" );
});

module("additional methods");

test("phone (us)", function() {
Expand Down

0 comments on commit 17a56ba

Please sign in to comment.