Skip to content

Commit e9fd71d

Browse files
skjelbekstaabm
authored andcommitted
Core: Unhighlighting field if already highlighted when using remote rule.
Closes jquery-validation#1712. Refs jquery-validation#1669, jquery-validation#1375, jquery-validation#12.
1 parent 5fc3585 commit e9fd71d

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

src/core.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,12 +630,16 @@ $.extend( $.validator, {
630630
return $( this.settings.errorElement + "." + errorClass, this.errorContext );
631631
},
632632

633-
reset: function() {
633+
resetInternals: function() {
634634
this.successList = [];
635635
this.errorList = [];
636636
this.errorMap = {};
637637
this.toShow = $( [] );
638638
this.toHide = $( [] );
639+
},
640+
641+
reset: function() {
642+
this.resetInternals();
639643
this.currentElements = $( [] );
640644
},
641645

@@ -1445,7 +1449,8 @@ $.extend( $.validator, {
14451449
validator.settings.messages[ element.name ][ method ] = previous.originalMessage;
14461450
if ( valid ) {
14471451
submitted = validator.formSubmitted;
1448-
validator.prepareElement( element );
1452+
validator.resetInternals();
1453+
validator.toHide = validator.errorsFor( element );
14491454
validator.formSubmitted = submitted;
14501455
validator.successList.push( element );
14511456
validator.invalid[ element.name ] = false;

test/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@ <h3></h3>
410410
<form id="add-method-remote">
411411
<input id="add-method-username" type="text" name="username" value="" data-rule-workemail="somevalue" required />
412412
</form>
413+
<form id="testForm25">
414+
<input type="text" data-rule-required="true" title="something25" name="something25" id="something25" value="">
415+
</form>
413416
</div>
414417
</body>
415418
</html>

test/methods.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,57 @@ test( "remote, highlight all invalid fields", function( assert ) {
618618
done();
619619
}, 500 );
620620
} );
621+
test( "remote, unhighlighted should be invoked after being highlighted/invalid", function( assert ) {
622+
expect( 6 );
623+
624+
var done1 = assert.async(),
625+
done2 = assert.async(),
626+
$form = $( "#testForm25" ),
627+
$somethingField = $form.find( "input[name='something25']" ),
628+
responseText = "false",
629+
response = function() { return responseText; },
630+
validateOptions = {
631+
highlight: function( e ) {
632+
$( e ).addClass( "error" );
633+
ok( true, "highlight should be called" );
634+
},
635+
unhighlight: function( e ) {
636+
$( e ).removeClass( "error" );
637+
ok( true, "unhighlight should be called" );
638+
},
639+
rules: {
640+
something25: {
641+
required: true,
642+
remote: {
643+
url: "response.php",
644+
type: "post",
645+
data: {
646+
responseText: response
647+
},
648+
async: false
649+
}
650+
}
651+
}
652+
};
653+
654+
$somethingField.val( "something value" );
655+
var v = $form.validate( validateOptions );
656+
v.element( $somethingField );
657+
658+
setTimeout( function() {
659+
equal( $somethingField.hasClass( "error" ), true, "Field 'something' should have the error class" );
660+
done1();
661+
$somethingField.val( "something value 2" );
662+
responseText = "true";
663+
664+
v.element( $somethingField );
665+
666+
setTimeout( function() {
667+
equal( $somethingField.hasClass( "error" ), false, "Field 'something' should not have the error class" );
668+
done2();
669+
}, 500 );
670+
}, 500 );
671+
} );
621672

622673
test( "Fix #697: remote validation uses wrong error messages", function( assert ) {
623674
var e = $( "#username" ),

test/test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ $.mockjax( {
4242
$.mockjax( {
4343
url: "response.php",
4444
response: function( settings ) {
45-
this.responseText = settings.data.responseText || "";
45+
var responseText = settings.data.responseText;
46+
if ( responseText ) {
47+
if ( typeof responseText === "function" ) {
48+
this.responseText = responseText();
49+
} else {
50+
this.responseText = responseText;
51+
}
52+
} else {
53+
this.responseText = "";
54+
}
4655
this.responseStatus = settings.data.responseStatus || 200;
4756
this.responseTime = settings.data.responseTime || 100;
4857
}

0 commit comments

Comments
 (0)