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

Commit e53554a

Browse files
committed
fix(NgModel): make sure the required validator uses the $validators pipeline
Fixes #5164
1 parent a8c7cb8 commit e53554a

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
@@ -1824,6 +1824,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
18241824
*/
18251825
this.$commitViewValue = function() {
18261826
var viewValue = ctrl.$viewValue;
1827+
18271828
$timeout.cancel(pendingDebounce);
18281829
if (ctrl.$$lastCommittedViewValue === viewValue) {
18291830
return;
@@ -2179,21 +2180,12 @@ var requiredDirective = function() {
21792180
if (!ctrl) return;
21802181
attr.required = true; // force truthy in case we are on non input element
21812182

2182-
var validator = function(value) {
2183-
if (attr.required && ctrl.$isEmpty(value)) {
2184-
ctrl.$setValidity('required', false);
2185-
return;
2186-
} else {
2187-
ctrl.$setValidity('required', true);
2188-
return value;
2189-
}
2183+
ctrl.$validators.required = function(modelValue, viewValue) {
2184+
return !attr.required || !ctrl.$isEmpty(viewValue);
21902185
};
21912186

2192-
ctrl.$formatters.push(validator);
2193-
ctrl.$parsers.unshift(validator);
2194-
21952187
attr.$observe('required', function() {
2196-
validator(ctrl.$viewValue);
2188+
ctrl.$validate();
21972189
});
21982190
}
21992191
};

test/ng/directive/inputSpec.js

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

26112611
scope.$apply(function() {
2612-
scope.name = '';
2612+
scope.name = null;
26132613
});
26142614

26152615
expect(inputElm).toBeInvalid();

0 commit comments

Comments
 (0)