From 52928bb992958ca15adf257b260cbbe404c0ce80 Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Wed, 24 Sep 2014 17:20:50 -0400 Subject: [PATCH] fix(input): don't parse if viewValue is undefined Closes #9106 --- src/ng/directive/input.js | 19 +++++++++++-------- test/ng/directive/inputSpec.js | 12 ++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 4a50d502746e..b8ceea7e546a 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -2053,14 +2053,17 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ }; this.$$parseAndValidate = function() { - var parserValid = true, - viewValue = ctrl.$$lastCommittedViewValue, - modelValue = viewValue; - for(var i = 0; i < ctrl.$parsers.length; i++) { - modelValue = ctrl.$parsers[i](modelValue); - if (isUndefined(modelValue)) { - parserValid = false; - break; + var viewValue = ctrl.$$lastCommittedViewValue; + var modelValue = viewValue; + var parserValid = isUndefined(modelValue) ? undefined : true; + + if (parserValid) { + for(var i = 0; i < ctrl.$parsers.length; i++) { + modelValue = ctrl.$parsers[i](modelValue); + if (isUndefined(modelValue)) { + parserValid = false; + break; + } } } if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) { diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 0c43fb322eaf..8b443e046b77 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -3717,6 +3717,18 @@ describe('input', function() { expect(inputElm).toBeInvalid(); expect(scope.form.alias.$error.required).toBeTruthy(); }); + + it('should not invalidate number if ng-required=false and model is undefined', function() { + compileInput(''); + + scope.$apply("required = false"); + + expect(inputElm).toBeValid(); + + scope.$apply("required = true"); + + expect(inputElm).toBeInvalid(); + }); }); describe('minlength', function() {