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
This commit is contained in:
Caitlin Potter
2014-09-24 17:20:50 -04:00
parent e81ae1464d
commit 92f05e5a59
2 changed files with 19 additions and 8 deletions

View File

@@ -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)) {

View File

@@ -3717,6 +3717,14 @@ 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('<input type="number" ng-model="value" name="alias" ng-required="required">');
scope.$apply("required = false");
expect(inputElm).toBeValid();
});
});
describe('minlength', function() {