Skip to content

Commit bbcda9c

Browse files
committed
fix(NgModel): make sure the ngMinlength and ngMaxlength validators use the $validators pipeline
Fixes angular#6304
1 parent f790fd4 commit bbcda9c

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
@@ -1006,23 +1006,17 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10061006
// min length validator
10071007
if (attr.ngMinlength) {
10081008
var minlength = int(attr.ngMinlength);
1009-
var minLengthValidator = function(value) {
1010-
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) || value.length >= minlength, value);
1009+
ctrl.$validators.minlength = function(value) {
1010+
return ctrl.$isEmpty(value) || value.length >= minlength;
10111011
};
1012-
1013-
ctrl.$parsers.push(minLengthValidator);
1014-
ctrl.$formatters.push(minLengthValidator);
10151012
}
10161013

10171014
// max length validator
10181015
if (attr.ngMaxlength) {
10191016
var maxlength = int(attr.ngMaxlength);
1020-
var maxLengthValidator = function(value) {
1021-
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) || value.length <= maxlength, value);
1017+
ctrl.$validators.maxlength = function(value) {
1018+
return ctrl.$isEmpty(value) || value.length <= maxlength;
10221019
};
1023-
1024-
ctrl.$parsers.push(maxLengthValidator);
1025-
ctrl.$formatters.push(maxLengthValidator);
10261020
}
10271021
}
10281022

test/ng/directive/inputSpec.js

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

12751275
describe('minlength', function() {
12761276

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

12801280
changeInputValueTo('aa');
1281-
expect(scope.value).toBeUndefined();
1281+
expect(inputElm).toBeInvalid();
12821282

12831283
changeInputValueTo('aaa');
1284-
expect(scope.value).toBe('aaa');
1284+
expect(inputElm).toBeValid();
12851285
});
12861286
});
12871287

12881288

12891289
describe('maxlength', function() {
12901290

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

12941294
changeInputValueTo('aaaaaaaa');
1295-
expect(scope.value).toBeUndefined();
1295+
expect(inputElm).toBeInvalid();
12961296

12971297
changeInputValueTo('aaa');
1298-
expect(scope.value).toBe('aaa');
1298+
expect(inputElm).toBeValid();
12991299
});
13001300
});
13011301

0 commit comments

Comments
 (0)