Skip to content

Commit f93e1de

Browse files
AndrewRayCodejzaefferer
authored andcommitted
Core: Trigger success on optional but have other successful validators
The comment in the code itself says "when there are no other rules" but does nothing to enforce that comment. Fixes gh-851 Closes gh-852
1 parent adbc636 commit f93e1de

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/core.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,9 @@ $.extend($.validator, {
552552
element = this.validationTargetFor( this.clean( element ) );
553553

554554
var rules = $(element).rules();
555+
var rulesCount = $.map( rules, function(n, i) {
556+
return i;
557+
}).length;
555558
var dependencyMismatch = false;
556559
var val = this.elementValue(element);
557560
var result;
@@ -564,7 +567,7 @@ $.extend($.validator, {
564567

565568
// if a method indicates that the field is optional and therefore valid,
566569
// don't mark it as valid when there are no other rules
567-
if ( result === "dependency-mismatch" ) {
570+
if ( result === "dependency-mismatch" && rulesCount === 1 ) {
568571
dependencyMismatch = true;
569572
continue;
570573
}

test/test.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,8 @@ test("successlist", function() {
902902
equal(0, v.successList.length);
903903
});
904904

905-
test("success isn't called for optional elements", function() {
905+
906+
test("success isn't called for optional elements with no other rules", function() {
906907
expect(4);
907908
equal( "", $("#firstname").removeAttr("data-rule-required").removeAttr("data-rule-minlength").val() );
908909
$("#something").remove();
@@ -913,7 +914,7 @@ test("success isn't called for optional elements", function() {
913914
ok( false, "don't call success for optional elements!" );
914915
},
915916
rules: {
916-
firstname: "email"
917+
firstname: { required: false }
917918
}
918919
});
919920
equal( 0, $("#testForm1 label").size() );
@@ -923,6 +924,31 @@ test("success isn't called for optional elements", function() {
923924
equal( 0, $("#testForm1 label").size() );
924925
});
925926

927+
test("success is called for optional elements with other rules", function() {
928+
expect(1);
929+
930+
$.validator.addMethod("custom1", function() {
931+
return true;
932+
}, "");
933+
934+
var v = $("#testForm1clean").validate({
935+
success: function() {
936+
ok( true, "success called correctly!" );
937+
},
938+
rules: {
939+
firstname: {
940+
required: false,
941+
custom1: true
942+
}
943+
}
944+
});
945+
946+
$("#firstnamec").valid();
947+
948+
delete $.validator.methods.custom1;
949+
});
950+
951+
926952
test("success callback with element", function() {
927953
expect(1);
928954
var v = $("#userForm").validate({

0 commit comments

Comments
 (0)