mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-06-15 01:49:05 +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() {
|
this.$$parseAndValidate = function() {
|
||||||
var parserValid = true,
|
var viewValue = ctrl.$$lastCommittedViewValue;
|
||||||
viewValue = ctrl.$$lastCommittedViewValue,
|
var modelValue = viewValue;
|
||||||
modelValue = viewValue;
|
var parserValid = isUndefined(modelValue) ? undefined : true;
|
||||||
for(var i = 0; i < ctrl.$parsers.length; i++) {
|
|
||||||
modelValue = ctrl.$parsers[i](modelValue);
|
if (parserValid) {
|
||||||
if (isUndefined(modelValue)) {
|
for(var i = 0; i < ctrl.$parsers.length; i++) {
|
||||||
parserValid = false;
|
modelValue = ctrl.$parsers[i](modelValue);
|
||||||
break;
|
if (isUndefined(modelValue)) {
|
||||||
|
parserValid = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
|
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
|
||||||
|
|||||||
@@ -3717,6 +3717,14 @@ describe('input', function() {
|
|||||||
expect(inputElm).toBeInvalid();
|
expect(inputElm).toBeInvalid();
|
||||||
expect(scope.form.alias.$error.required).toBeTruthy();
|
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() {
|
describe('minlength', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user