Skip to content

Commit

Permalink
Core: Trigger success on optional but have other successful validators
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AndrewRayCode authored and jzaefferer committed Jan 14, 2014
1 parent adbc636 commit f93e1de
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,9 @@ $.extend($.validator, {
element = this.validationTargetFor( this.clean( element ) );

var rules = $(element).rules();
var rulesCount = $.map( rules, function(n, i) {
return i;
}).length;
var dependencyMismatch = false;
var val = this.elementValue(element);
var result;
Expand All @@ -564,7 +567,7 @@ $.extend($.validator, {

// if a method indicates that the field is optional and therefore valid,
// don't mark it as valid when there are no other rules
if ( result === "dependency-mismatch" ) {
if ( result === "dependency-mismatch" && rulesCount === 1 ) {
dependencyMismatch = true;
continue;
}
Expand Down
30 changes: 28 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,8 @@ test("successlist", function() {
equal(0, v.successList.length);
});

test("success isn't called for optional elements", function() {

test("success isn't called for optional elements with no other rules", function() {
expect(4);
equal( "", $("#firstname").removeAttr("data-rule-required").removeAttr("data-rule-minlength").val() );
$("#something").remove();
Expand All @@ -913,7 +914,7 @@ test("success isn't called for optional elements", function() {
ok( false, "don't call success for optional elements!" );
},
rules: {
firstname: "email"
firstname: { required: false }
}
});
equal( 0, $("#testForm1 label").size() );
Expand All @@ -923,6 +924,31 @@ test("success isn't called for optional elements", function() {
equal( 0, $("#testForm1 label").size() );
});

test("success is called for optional elements with other rules", function() {
expect(1);

$.validator.addMethod("custom1", function() {
return true;
}, "");

var v = $("#testForm1clean").validate({
success: function() {
ok( true, "success called correctly!" );
},
rules: {
firstname: {
required: false,
custom1: true
}
}
});

$("#firstnamec").valid();

delete $.validator.methods.custom1;
});


test("success callback with element", function() {
expect(1);
var v = $("#userForm").validate({
Expand Down

0 comments on commit f93e1de

Please sign in to comment.