Skip to content

Commit 999aa15

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

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
@@ -1002,23 +1002,17 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10021002
// min length validator
10031003
if (attr.ngMinlength) {
10041004
var minlength = int(attr.ngMinlength);
1005-
var minLengthValidator = function(value) {
1006-
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) || value.length >= minlength, value);
1005+
ctrl.$validators.minlength = function(value) {
1006+
return ctrl.$isEmpty(value) || value.length >= minlength;
10071007
};
1008-
1009-
ctrl.$parsers.push(minLengthValidator);
1010-
ctrl.$formatters.push(minLengthValidator);
10111008
}
10121009

10131010
// max length validator
10141011
if (attr.ngMaxlength) {
10151012
var maxlength = int(attr.ngMaxlength);
1016-
var maxLengthValidator = function(value) {
1017-
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) || value.length <= maxlength, value);
1013+
ctrl.$validators.maxlength = function(value) {
1014+
return ctrl.$isEmpty(value) || value.length <= maxlength;
10181015
};
1019-
1020-
ctrl.$parsers.push(maxLengthValidator);
1021-
ctrl.$formatters.push(maxLengthValidator);
10221016
}
10231017
}
10241018

test/ng/directive/inputSpec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1300,14 +1300,14 @@ describe('input', function() {
13001300

13011301
describe('minlength', function() {
13021302

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

13061306
changeInputValueTo('aa');
1307-
expect(scope.value).toBeUndefined();
1307+
expect(inputElm).toBeInvalid();
13081308

13091309
changeInputValueTo('aaa');
1310-
expect(scope.value).toBe('aaa');
1310+
expect(inputElm).toBeValid();
13111311
});
13121312

13131313
it('should listen on ng-minlength when minlength is observed', function() {
@@ -1328,14 +1328,14 @@ describe('input', function() {
13281328

13291329
describe('maxlength', function() {
13301330

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

13341334
changeInputValueTo('aaaaaaaa');
1335-
expect(scope.value).toBeUndefined();
1335+
expect(inputElm).toBeInvalid();
13361336

13371337
changeInputValueTo('aaa');
1338-
expect(scope.value).toBe('aaa');
1338+
expect(inputElm).toBeValid();
13391339
});
13401340

13411341
it('should listen on ng-maxlength when maxlength is observed', function() {

0 commit comments

Comments
 (0)