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.

This patch addresses this by making use of the Infinity global when a value's length property is
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 Jan 27, 2014
1 parent 766b3d5 commit 572baa0
Show file tree
Hide file tree
Showing 2 changed files with 24 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 @@ -518,7 +518,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 : Infinity) >= minlength, value);
};

ctrl.$parsers.push(minLengthValidator);
Expand All @@ -529,7 +530,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 : -Infinity) <= maxlength, value);
};

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

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 @@ -747,6 +757,16 @@ describe('input', function() {
changeInputValueTo('aaa');
expect(scope.value).toBe('aaa');
});

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 572baa0

Please sign in to comment.