Skip to content

Commit bc663cc

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. 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 f40f54c commit bc663cc

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/ng/directive/input.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,8 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
998998
if (attr.ngMinlength) {
999999
var minlength = int(attr.ngMinlength);
10001000
var minLengthValidator = function(value) {
1001-
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) || value.length >= minlength, value);
1001+
return validate(ctrl, 'minlength', ctrl.$isEmpty(value) ||
1002+
(!isDefined(value.length) || value.length >= minlength), value);
10021003
};
10031004

10041005
ctrl.$parsers.push(minLengthValidator);
@@ -1009,7 +1010,8 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10091010
if (attr.ngMaxlength) {
10101011
var maxlength = int(attr.ngMaxlength);
10111012
var maxLengthValidator = function(value) {
1012-
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) || value.length <= maxlength, value);
1013+
return validate(ctrl, 'maxlength', ctrl.$isEmpty(value) ||
1014+
(!isDefined(value.length) || value.length <= maxlength), value);
10131015
};
10141016

10151017
ctrl.$parsers.push(maxLengthValidator);

test/ng/directive/inputSpec.js

+56
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,34 @@ describe('input', function() {
744744
changeInputValueTo('aaa');
745745
expect(scope.value).toBe('aaa');
746746
});
747+
748+
749+
it('should invalidate model values shorter than given minlength', function() {
750+
compileInput('<input type="text" ng-model="value" ng-minlength="5" />');
751+
752+
scope.$apply(function() {
753+
scope.value = "1234";
754+
});
755+
756+
expect(inputElm).toBeInvalid();
757+
758+
scope.$apply(function() {
759+
scope.value = "123456789";
760+
});
761+
762+
expect(inputElm).toBeValid();
763+
});
764+
765+
766+
it('should not invalidate model-values which are not string-like or array-like', function() {
767+
compileInput('<input type="text" ng-model="value" ng-minlength="3" />');
768+
769+
scope.$apply(function() {
770+
scope.value = 10;
771+
});
772+
773+
expect(inputElm).toBeValid();
774+
});
747775
});
748776

749777

@@ -758,6 +786,34 @@ describe('input', function() {
758786
changeInputValueTo('aaa');
759787
expect(scope.value).toBe('aaa');
760788
});
789+
790+
791+
it('should invalidate model values longer than given maxlength', function() {
792+
compileInput('<input type="text" ng-model="value" ng-maxlength="5" />');
793+
794+
scope.$apply(function() {
795+
scope.value = "123456789";
796+
});
797+
798+
expect(inputElm).toBeInvalid();
799+
800+
scope.$apply(function() {
801+
scope.value = "1234";
802+
});
803+
804+
expect(inputElm).toBeValid();
805+
});
806+
807+
808+
it('should not invalidate model-values which are not string-like or array-like', function() {
809+
compileInput('<input type="text" ng-model="value" ng-maxlength="3" />');
810+
811+
scope.$apply(function() {
812+
scope.value = 1000;
813+
});
814+
815+
expect(inputElm).toBeValid();
816+
});
761817
});
762818

763819

0 commit comments

Comments
 (0)