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

Commit 92f05e5

Browse files
committed
fix(ngModel): do not parse undefined viewValue when validating
Previously, if a viewValue had not yet been set on the element, it could incorrectly produce a parse error. This change prevents the parsers from running if a view value has not yet been committed. Closes #9106 Closes #9260
1 parent e81ae14 commit 92f05e5

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/ng/directive/input.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -2053,14 +2053,17 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
20532053
};
20542054

20552055
this.$$parseAndValidate = function() {
2056-
var parserValid = true,
2057-
viewValue = ctrl.$$lastCommittedViewValue,
2058-
modelValue = viewValue;
2059-
for(var i = 0; i < ctrl.$parsers.length; i++) {
2060-
modelValue = ctrl.$parsers[i](modelValue);
2061-
if (isUndefined(modelValue)) {
2062-
parserValid = false;
2063-
break;
2056+
var viewValue = ctrl.$$lastCommittedViewValue;
2057+
var modelValue = viewValue;
2058+
var parserValid = isUndefined(modelValue) ? undefined : true;
2059+
2060+
if (parserValid) {
2061+
for(var i = 0; i < ctrl.$parsers.length; i++) {
2062+
modelValue = ctrl.$parsers[i](modelValue);
2063+
if (isUndefined(modelValue)) {
2064+
parserValid = false;
2065+
break;
2066+
}
20642067
}
20652068
}
20662069
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {

test/ng/directive/inputSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -3717,6 +3717,14 @@ describe('input', function() {
37173717
expect(inputElm).toBeInvalid();
37183718
expect(scope.form.alias.$error.required).toBeTruthy();
37193719
});
3720+
3721+
it('should not invalidate number if ng-required=false and model is undefined', function() {
3722+
compileInput('<input type="number" ng-model="value" name="alias" ng-required="required">');
3723+
3724+
scope.$apply("required = false");
3725+
3726+
expect(inputElm).toBeValid();
3727+
});
37203728
});
37213729

37223730
describe('minlength', function() {

0 commit comments

Comments
 (0)