diff --git a/src/ng/directive/ngModel.js b/src/ng/directive/ngModel.js index 55310adceddd..a74e8a4bffae 100644 --- a/src/ng/directive/ngModel.js +++ b/src/ng/directive/ngModel.js @@ -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); }); diff --git a/test/ng/directive/ngModelSpec.js b/test/ng/directive/ngModelSpec.js index 37c633f13c97..b21f21baeea1 100644 --- a/test/ng/directive/ngModelSpec.js +++ b/test/ng/directive/ngModelSpec.js @@ -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) {