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

fix(ngModel): don't parse before validating if viewValue is undefined #9260

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ii = ctrl.$parsers.length ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no tests, but it's technically possible for the parsers array to grow while a parser is running, so changing that could be a breaking change.

We can do it but it might not be a good commit to do it in

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough

modelValue = ctrl.$parsers[i](modelValue);
if (isUndefined(modelValue)) {
parserValid = false;
break;
}
}
}
if (isNumber(ctrl.$modelValue) && isNaN(ctrl.$modelValue)) {
Expand Down
12 changes: 12 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<input type="number" ng-model="value" name="alias" ng-required="required">');

scope.$apply("required = false");

expect(inputElm).toBeValid();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this assertion does fail without this change


scope.$apply("required = true");

expect(inputElm).toBeInvalid();
});
});

describe('minlength', function() {
Expand Down