Skip to content

Commit 41d967a

Browse files
committed
refactor(ngModel): make sure the ngMinlength and ngMaxlength validators use the $validators pipeline
Fixes angular#6304
1 parent 01c0411 commit 41d967a

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

src/ng/directive/input.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -1015,23 +1015,17 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10151015
// min length validator
10161016
if (attr.ngMinlength) {
10171017
var minlength = int(attr.ngMinlength);
1018-
var minLengthValidator = function(value) {
1019-
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) || value.length >= minlength, value);
1018+
ctrl.$validators.minlength = function(value) {
1019+
return ctrl.$isEmpty(value) || value.length >= minlength;
10201020
};
1021-
1022-
ctrl.$parsers.push(minLengthValidator);
1023-
ctrl.$formatters.push(minLengthValidator);
10241021
}
10251022

10261023
// max length validator
10271024
if (attr.ngMaxlength) {
10281025
var maxlength = int(attr.ngMaxlength);
1029-
var maxLengthValidator = function(value) {
1030-
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) || value.length <= maxlength, value);
1026+
ctrl.$validators.maxlength = function(value) {
1027+
return ctrl.$isEmpty(value) || value.length <= maxlength;
10311028
};
1032-
1033-
ctrl.$parsers.push(maxLengthValidator);
1034-
ctrl.$formatters.push(maxLengthValidator);
10351029
}
10361030
}
10371031

test/ng/directive/inputSpec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1212,28 +1212,28 @@ describe('input', function() {
12121212

12131213
describe('minlength', function() {
12141214

1215-
it('should invalid shorter than given minlength', function() {
1215+
it('should invalidate values that are shorter than the given minlength', function() {
12161216
compileInput('<input type="text" ng-model="value" ng-minlength="3" />');
12171217

12181218
changeInputValueTo('aa');
1219-
expect(scope.value).toBeUndefined();
1219+
expect(inputElm).toBeInvalid();
12201220

12211221
changeInputValueTo('aaa');
1222-
expect(scope.value).toBe('aaa');
1222+
expect(inputElm).toBeValid();
12231223
});
12241224
});
12251225

12261226

12271227
describe('maxlength', function() {
12281228

1229-
it('should invalid shorter than given maxlength', function() {
1229+
it('should invalidate values that are longer than the given maxlength', function() {
12301230
compileInput('<input type="text" ng-model="value" ng-maxlength="5" />');
12311231

12321232
changeInputValueTo('aaaaaaaa');
1233-
expect(scope.value).toBeUndefined();
1233+
expect(inputElm).toBeInvalid();
12341234

12351235
changeInputValueTo('aaa');
1236-
expect(scope.value).toBe('aaa');
1236+
expect(inputElm).toBeValid();
12371237
});
12381238
});
12391239

0 commit comments

Comments
 (0)