Skip to content

Commit

Permalink
fix(input): ngMinlength/ngMaxlength should not invalidate non-stringl…
Browse files Browse the repository at this point in the history
…ike values

A regression reported in angular#5936 shows that prior to cdc4d48, an
input with an ngMinlength or ngMaxlength validator would not invalidate values where value.length
was undefined.

The behaviour of this validator is still incorrect in the case of objects with a length property
which are not arrays or strings, and will most likely remain that way. This cannot change as it is
possibly desirable to use ngMinlength/ngMaxlength in conjunction with ngList.

Closes angular#5936
Closes angular#6000
  • Loading branch information
caitp committed Mar 19, 2014
1 parent f40f54c commit bc663cc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,8 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
if (attr.ngMinlength) {
var minlength = int(attr.ngMinlength);
var minLengthValidator = function(value) {
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) || value.length >= minlength, value);
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) ||
(!isDefined(value.length) || value.length >= minlength), value);
};

ctrl.$parsers.push(minLengthValidator);
Expand All @@ -1009,7 +1010,8 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
if (attr.ngMaxlength) {
var maxlength = int(attr.ngMaxlength);
var maxLengthValidator = function(value) {
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) || value.length <= maxlength, value);
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) ||
(!isDefined(value.length) || value.length <= maxlength), value);
};

ctrl.$parsers.push(maxLengthValidator);
Expand Down
56 changes: 56 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,34 @@ describe('input', function() {
changeInputValueTo('aaa');
expect(scope.value).toBe('aaa');
});


it('should invalidate model values shorter than given minlength', function() {
compileInput('<input type="text" ng-model="value" ng-minlength="5" />');

scope.$apply(function() {
scope.value = "1234";
});

expect(inputElm).toBeInvalid();

scope.$apply(function() {
scope.value = "123456789";
});

expect(inputElm).toBeValid();
});


it('should not invalidate model-values which are not string-like or array-like', function() {
compileInput('<input type="text" ng-model="value" ng-minlength="3" />');

scope.$apply(function() {
scope.value = 10;
});

expect(inputElm).toBeValid();
});
});


Expand All @@ -758,6 +786,34 @@ describe('input', function() {
changeInputValueTo('aaa');
expect(scope.value).toBe('aaa');
});


it('should invalidate model values longer than given maxlength', function() {
compileInput('<input type="text" ng-model="value" ng-maxlength="5" />');

scope.$apply(function() {
scope.value = "123456789";
});

expect(inputElm).toBeInvalid();

scope.$apply(function() {
scope.value = "1234";
});

expect(inputElm).toBeValid();
});


it('should not invalidate model-values which are not string-like or array-like', function() {
compileInput('<input type="text" ng-model="value" ng-maxlength="3" />');

scope.$apply(function() {
scope.value = 1000;
});

expect(inputElm).toBeValid();
});
});


Expand Down

0 comments on commit bc663cc

Please sign in to comment.