Skip to content

Commit 960d32b

Browse files
committed
fix(NgModel): make sure the required validator uses the $validators pipeline
Fixes angular#5164
1 parent 678764c commit 960d32b

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
@@ -1781,6 +1781,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
17811781
*/
17821782
this.$commitViewValue = function() {
17831783
var viewValue = ctrl.$viewValue;
1784+
17841785
$timeout.cancel(pendingDebounce);
17851786
if (ctrl.$$lastCommittedViewValue === viewValue) {
17861787
return;
@@ -2130,21 +2131,12 @@ var requiredDirective = function() {
21302131
if (!ctrl) return;
21312132
attr.required = true; // force truthy in case we are on non input element
21322133

2133-
var validator = function(value) {
2134-
if (attr.required && ctrl.$isEmpty(value)) {
2135-
ctrl.$setValidity('required', false);
2136-
return;
2137-
} else {
2138-
ctrl.$setValidity('required', true);
2139-
return value;
2140-
}
2134+
ctrl.$validators.required = function(modelValue, viewValue) {
2135+
return !attr.required || !ctrl.$isEmpty(viewValue);
21412136
};
21422137

2143-
ctrl.$formatters.push(validator);
2144-
ctrl.$parsers.unshift(validator);
2145-
21462138
attr.$observe('required', function() {
2147-
validator(ctrl.$viewValue);
2139+
ctrl.$validate();
21482140
});
21492141
}
21502142
};

test/ng/directive/inputSpec.js

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

25662566
scope.$apply(function() {
2567-
scope.name = '';
2567+
scope.name = null;
25682568
});
25692569

25702570
expect(inputElm).toBeInvalid();

0 commit comments

Comments
 (0)