Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7df9181

Browse files
committedMay 27, 2014
fix(NgModel): make sure the required validator uses the $validators pipeline
Fixes angular#5164
1 parent 2f01f12 commit 7df9181

File tree

2 files changed

+5
-13
lines changed

2 files changed

+5
-13
lines changed
 

‎src/ng/directive/input.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
17861786
*/
17871787
this.$commitViewValue = function() {
17881788
var viewValue = ctrl.$viewValue;
1789+
17891790
$timeout.cancel(pendingDebounce);
17901791
if (ctrl.$$lastCommittedViewValue === viewValue) {
17911792
return;
@@ -2135,21 +2136,12 @@ var requiredDirective = function() {
21352136
if (!ctrl) return;
21362137
attr.required = true; // force truthy in case we are on non input element
21372138

2138-
var validator = function(value) {
2139-
if (attr.required && ctrl.$isEmpty(value)) {
2140-
ctrl.$setValidity('required', false);
2141-
return;
2142-
} else {
2143-
ctrl.$setValidity('required', true);
2144-
return value;
2145-
}
2139+
ctrl.$validators.required = function(modelValue, viewValue) {
2140+
return !attr.required || !ctrl.$isEmpty(viewValue);
21462141
};
21472142

2148-
ctrl.$formatters.push(validator);
2149-
ctrl.$parsers.unshift(validator);
2150-
21512143
attr.$observe('required', function() {
2152-
validator(ctrl.$viewValue);
2144+
ctrl.$validate();
21532145
});
21542146
}
21552147
};

‎test/ng/directive/inputSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2510,7 +2510,7 @@ describe('input', function() {
25102510
compileInput('<input type="text" ng-model="name" name="alias" required />');
25112511

25122512
scope.$apply(function() {
2513-
scope.name = '';
2513+
scope.name = null;
25142514
});
25152515

25162516
expect(inputElm).toBeInvalid();

0 commit comments

Comments
 (0)
Please sign in to comment.