mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-06-13 17:15:51 +08:00
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:
@@ -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)) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user