Skip to content

Commit

Permalink
Merge pull request jquery-validation#1628 from alokito/pendingClass2
Browse files Browse the repository at this point in the history
Adds pending class to elements while remote operations
  • Loading branch information
staabm committed Oct 29, 2015
2 parents c35eff4 + c52da5b commit 4afc317
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ $.extend( $.validator, {
groups: {},
rules: {},
errorClass: "error",
pendingClass: "pending",
validClass: "valid",
errorElement: "label",
focusCleanup: false,
Expand Down Expand Up @@ -938,6 +939,7 @@ $.extend( $.validator, {
startRequest: function( element ) {
if ( !this.pending[ element.name ] ) {
this.pendingRequest++;
$( element ).addClass( this.settings.pendingClass );
this.pending[ element.name ] = true;
}
},
Expand All @@ -949,6 +951,7 @@ $.extend( $.validator, {
this.pendingRequest = 0;
}
delete this.pending[ element.name ];
$( element ).removeClass( this.settings.pendingClass );
if ( valid && this.pendingRequest === 0 && this.formSubmitted && this.form() ) {
$( this.currentForm ).submit();
this.formSubmitted = false;
Expand Down
23 changes: 23 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,29 @@ asyncTest( "remote", function() {
strictEqual( v.element( e ), true, "still invalid, because remote validation must block until it returns; dependency-mismatch considered as valid though" );
} );

asyncTest( "remote, pending class added to element while call outstanding", function() {
expect( 3 );
var e = $( "#username" ),
v = $( "#userForm" ).validate( {
rules: {
username: {
remote: {
url: "users.php",
complete: function() {
strictEqual( e.hasClass( "pending" ), false, "not pending since ajax call complete" );
start();
}
}
}
}
} );
strictEqual( e.hasClass( "pending" ), false, "not pending since no data entered" );
e.val( "Peter" );
// this fires off the validation:
v.element( e );
strictEqual( e.hasClass( "pending" ), true, "pending while validation outstanding" );
} );

asyncTest( "remote, customized ajax options", function() {
expect( 2 );
$( "#userForm" ).validate( {
Expand Down

0 comments on commit 4afc317

Please sign in to comment.