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

Commit 5b8e7ec

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

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
@@ -1004,23 +1004,17 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10041004
// min length validator
10051005
if (attr.ngMinlength) {
10061006
var minlength = int(attr.ngMinlength);
1007-
var minLengthValidator = function(value) {
1008-
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) || value.length >= minlength, value);
1007+
ctrl.$validators.minlength = function(value) {
1008+
return ctrl.$isEmpty(value) || value.length >= minlength;
10091009
};
1010-
1011-
ctrl.$parsers.push(minLengthValidator);
1012-
ctrl.$formatters.push(minLengthValidator);
10131010
}
10141011

10151012
// max length validator
10161013
if (attr.ngMaxlength) {
10171014
var maxlength = int(attr.ngMaxlength);
1018-
var maxLengthValidator = function(value) {
1019-
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) || value.length <= maxlength, value);
1015+
ctrl.$validators.maxlength = function(value) {
1016+
return ctrl.$isEmpty(value) || value.length <= maxlength;
10201017
};
1021-
1022-
ctrl.$parsers.push(maxLengthValidator);
1023-
ctrl.$formatters.push(maxLengthValidator);
10241018
}
10251019
}
10261020

test/ng/directive/inputSpec.js

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

13461346
describe('minlength', function() {
13471347

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

13511351
changeInputValueTo('aa');
1352-
expect(scope.value).toBeUndefined();
1352+
expect(inputElm).toBeInvalid();
13531353

13541354
changeInputValueTo('aaa');
1355-
expect(scope.value).toBe('aaa');
1355+
expect(inputElm).toBeValid();
13561356
});
13571357

13581358
it('should listen on ng-minlength when minlength is observed', function() {
@@ -1373,14 +1373,14 @@ describe('input', function() {
13731373

13741374
describe('maxlength', function() {
13751375

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

13791379
changeInputValueTo('aaaaaaaa');
1380-
expect(scope.value).toBeUndefined();
1380+
expect(inputElm).toBeInvalid();
13811381

13821382
changeInputValueTo('aaa');
1383-
expect(scope.value).toBe('aaa');
1383+
expect(inputElm).toBeValid();
13841384
});
13851385

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

0 commit comments

Comments
 (0)