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

Commit 5c1fdff

Browse files
gkalpakpkozlowski-opensource
authored andcommitted
feat(ngMaxlength): add support for disabling max length limit
Previously, setting the maxlength to a negative number, would make all input values invalid (since their length should be less than maxlength, which is impossible). This commit changes the behaviour of maxlength/ngMaxlength, effectively disabling the maxlength validation (always returning true) when maxlength is set to a negative number. This is more inline to how the HTML5 `maxlength` attribute works (both in browsers and according to the spec: http://dev.w3.org/html5/spec-preview/attributes-common-to-form-controls.html#attr-fe-maxlength). Related to #9874 Closes #9995
1 parent 891acf4 commit 5c1fdff

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/ng/directive/input.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2696,7 +2696,7 @@ var maxlengthDirective = function() {
26962696
ctrl.$validate();
26972697
});
26982698
ctrl.$validators.maxlength = function(modelValue, viewValue) {
2699-
return ctrl.$isEmpty(modelValue) || viewValue.length <= maxlength;
2699+
return (maxlength < 0) || ctrl.$isEmpty(modelValue) || (viewValue.length <= maxlength);
27002700
};
27012701
}
27022702
};

test/ng/directive/inputSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,16 @@ describe('input', function() {
24952495
expect(inputElm).toBeValid();
24962496
});
24972497

2498+
it('should accept values of any length when maxlength is negative', function() {
2499+
compileInput('<input type="text" ng-model="value" ng-maxlength="-1" />');
2500+
2501+
changeInputValueTo('');
2502+
expect(inputElm).toBeValid();
2503+
2504+
changeInputValueTo('aaaaaaaaaa');
2505+
expect(inputElm).toBeValid();
2506+
});
2507+
24982508
it('should listen on ng-maxlength when maxlength is observed', function() {
24992509
var value = 0;
25002510
compileInput('<input type="text" ng-model="value" ng-maxlength="max" attr-capture />');

0 commit comments

Comments
 (0)