Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Patch 14734 boolean sync validators #15208

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/directive/ngModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ NgModelController.prototype = {
function processSyncValidators() {
var syncValidatorsValid = true;
forEach(that.$validators, function(validator, name) {
var result = validator(modelValue, viewValue);
var result = Boolean(validator(modelValue, viewValue));
syncValidatorsValid = syncValidatorsValid && result;
setValidity(name, result);
});
Expand Down
26 changes: 26 additions & 0 deletions test/ng/directive/ngModelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,32 @@ describe('ngModel', function() {
expect(ctrl.$valid).toBe(true);
});

it('should treat all responses as boolean for synchronous validators', function() {
var expectValid = function(value, expected) {
ctrl.$modelValue = undefined;
ctrl.$validators.a = valueFn(value);

ctrl.$validate();
expect(ctrl.$valid).toBe(expected);
};

// False tests
expectValid(false, false);
expectValid(undefined, false);
expectValid(null, false);
expectValid(0, false);
expectValid(NaN, false);
expectValid('', false);

// True tests
expectValid(true, true);
expectValid(1, true);
expectValid('0', true);
expectValid('false', true);
expectValid([], true);
expectValid({}, true);
});


it('should register invalid validations on the $error object', function() {
var curry = function(v) {
Expand Down