Skip to content

Commit 572baa0

Browse files
committed
fix(input): ngMinlength/ngMaxlength should not invalidate non-stringlike 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
1 parent 766b3d5 commit 572baa0

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/ng/directive/input.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,8 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
518518
if (attr.ngMinlength) {
519519
var minlength = int(attr.ngMinlength);
520520
var minLengthValidator = function(value) {
521-
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) || value.length >= minlength, value);
521+
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) ||
522+
(isDefined(value.length) ? value.length : Infinity) >= minlength, value);
522523
};
523524

524525
ctrl.$parsers.push(minLengthValidator);
@@ -529,7 +530,8 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
529530
if (attr.ngMaxlength) {
530531
var maxlength = int(attr.ngMaxlength);
531532
var maxLengthValidator = function(value) {
532-
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) || value.length <= maxlength, value);
533+
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) ||
534+
(isDefined(value.length) ? value.length : -Infinity) <= maxlength, value);
533535
};
534536

535537
ctrl.$parsers.push(maxLengthValidator);

test/ng/directive/inputSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,16 @@ describe('input', function() {
733733
changeInputValueTo('aaa');
734734
expect(scope.value).toBe('aaa');
735735
});
736+
737+
it('should not invalidate model-values which are not string-like or array-like', function() {
738+
compileInput('<input type="text" ng-model="value" ng-minlength="3" />');
739+
740+
scope.$apply(function() {
741+
scope.value = 10;
742+
});
743+
744+
expect(inputElm).toBeValid();
745+
});
736746
});
737747

738748

@@ -747,6 +757,16 @@ describe('input', function() {
747757
changeInputValueTo('aaa');
748758
expect(scope.value).toBe('aaa');
749759
});
760+
761+
it('should not invalidate model-values which are not string-like or array-like', function() {
762+
compileInput('<input type="text" ng-model="value" ng-maxlength="3" />');
763+
764+
scope.$apply(function() {
765+
scope.value = 1000;
766+
});
767+
768+
expect(inputElm).toBeValid();
769+
});
750770
});
751771

752772

0 commit comments

Comments
 (0)