Skip to content

Commit 9385017

Browse files
committed
refactor(NgModel): make sure the required validator uses the $validators pipeline
1 parent 81e2aba commit 9385017

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

src/ng/directive/input.js

+3-12
Original file line numberDiff line numberDiff line change
@@ -2131,21 +2131,12 @@ var requiredDirective = function() {
21312131
if (!ctrl) return;
21322132
attr.required = true; // force truthy in case we are on non input element
21332133

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

2144-
ctrl.$formatters.push(validator);
2145-
ctrl.$parsers.unshift(validator);
2146-
21472138
attr.$observe('required', function() {
2148-
validator(ctrl.$viewValue);
2139+
ctrl.$validateLater();
21492140
});
21502141
}
21512142
};

test/ng/directive/inputSpec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2396,7 +2396,7 @@ describe('input', function() {
23962396
it('should be invalid if required and empty', function() {
23972397
compileInput('<input type="text" ng-list ng-model="list" required>');
23982398
changeInputValueTo('');
2399-
expect(scope.list).toBeUndefined();
2399+
expect(scope.list).toEqual([]);
24002400
expect(inputElm).toBeInvalid();
24012401
changeInputValueTo('a,b');
24022402
expect(scope.list).toEqual(['a','b']);
@@ -2470,7 +2470,7 @@ describe('input', function() {
24702470
});
24712471

24722472

2473-
it('should be $invalid but $pristine if not touched', function() {
2473+
it('should be considered $pristine if the value is empty', function() {
24742474
compileInput('<input type="text" ng-model="name" name="alias" required />');
24752475

24762476
scope.$apply(function() {
@@ -2482,7 +2482,7 @@ describe('input', function() {
24822482

24832483
changeInputValueTo('');
24842484
expect(inputElm).toBeInvalid();
2485-
expect(inputElm).toBeDirty();
2485+
expect(inputElm).toBePristine();
24862486
});
24872487

24882488

0 commit comments

Comments
 (0)