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

Commit c4ff166

Browse files
committed
fix(ngModelController): always use the most recent viewValue for validation
This fixes issues where a parser calls $setViewValue. This is a common strategy for manipulating the $viewValue while the user is entering data into an input field. When the $viewValue was changed inside the parser, the new viewValue would be committed, parsed and used for validation. The original parser however would run after that and pass the original (outdated) viewValue on to the validators, which could cause false positives, e.g. for minlength. Fixes #10126 Fixes #10299
1 parent 9fa73cb commit c4ff166

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/ng/directive/input.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2200,11 +2200,15 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
22002200
var prevModelValue = ctrl.$modelValue;
22012201
var allowInvalid = ctrl.$options && ctrl.$options.allowInvalid;
22022202
ctrl.$$rawModelValue = modelValue;
2203+
22032204
if (allowInvalid) {
22042205
ctrl.$modelValue = modelValue;
22052206
writeToModelIfNeeded();
22062207
}
2207-
ctrl.$$runValidators(parserValid, modelValue, viewValue, function(allValid) {
2208+
2209+
// Pass the $$lastCommittedViewValue here, because the cached viewValue might be out of date.
2210+
// This can happen if e.g. $setViewValue is called from inside a parser
2211+
ctrl.$$runValidators(parserValid, modelValue, ctrl.$$lastCommittedViewValue, function(allValid) {
22082212
if (!allowInvalid) {
22092213
// Note: Don't check ctrl.$valid here, as we could have
22102214
// external validators (e.g. calculated on the server),

test/ng/directive/inputSpec.js

+50
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,56 @@ describe('NgModelController', function() {
10661066

10671067
dealoc(element);
10681068
}));
1069+
1070+
it('should always use the most recent $viewValue for validation', function() {
1071+
ctrl.$parsers.push(function(value) {
1072+
if (value && value.substr(-1) === 'b') {
1073+
value = 'a';
1074+
ctrl.$setViewValue(value);
1075+
ctrl.$render();
1076+
}
1077+
1078+
return value;
1079+
});
1080+
1081+
ctrl.$validators.mock = function(modelValue) {
1082+
return true;
1083+
};
1084+
1085+
spyOn(ctrl.$validators, 'mock').andCallThrough();
1086+
1087+
ctrl.$setViewValue('ab');
1088+
1089+
expect(ctrl.$validators.mock).toHaveBeenCalledWith('a', 'a');
1090+
expect(ctrl.$validators.mock.calls.length).toEqual(2);
1091+
});
1092+
1093+
it('should validate even if the modelValue did not change', function() {
1094+
ctrl.$parsers.push(function(value) {
1095+
if (value && value.substr(-1) === 'b') {
1096+
value = 'a';
1097+
}
1098+
1099+
return value;
1100+
});
1101+
1102+
ctrl.$validators.mock = function(modelValue) {
1103+
return true;
1104+
};
1105+
1106+
spyOn(ctrl.$validators, 'mock').andCallThrough();
1107+
1108+
ctrl.$setViewValue('a');
1109+
1110+
expect(ctrl.$validators.mock).toHaveBeenCalledWith('a', 'a');
1111+
expect(ctrl.$validators.mock.calls.length).toEqual(1);
1112+
1113+
ctrl.$setViewValue('ab');
1114+
1115+
expect(ctrl.$validators.mock).toHaveBeenCalledWith('a', 'ab');
1116+
expect(ctrl.$validators.mock.calls.length).toEqual(2);
1117+
});
1118+
10691119
});
10701120
});
10711121

0 commit comments

Comments
 (0)