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
This commit is contained in:
Georgios Kalpakas
2014-11-11 11:20:39 +02:00
committed by Pawel Kozlowski
parent 891acf4c20
commit 5c1fdff691
2 changed files with 11 additions and 1 deletions

View File

@@ -2696,7 +2696,7 @@ var maxlengthDirective = function() {
ctrl.$validate();
});
ctrl.$validators.maxlength = function(modelValue, viewValue) {
return ctrl.$isEmpty(modelValue) || viewValue.length <= maxlength;
return (maxlength < 0) || ctrl.$isEmpty(modelValue) || (viewValue.length <= maxlength);
};
}
};

View File

@@ -2495,6 +2495,16 @@ describe('input', function() {
expect(inputElm).toBeValid();
});
it('should accept values of any length when maxlength is negative', function() {
compileInput('<input type="text" ng-model="value" ng-maxlength="-1" />');
changeInputValueTo('');
expect(inputElm).toBeValid();
changeInputValueTo('aaaaaaaaaa');
expect(inputElm).toBeValid();
});
it('should listen on ng-maxlength when maxlength is observed', function() {
var value = 0;
compileInput('<input type="text" ng-model="value" ng-maxlength="max" attr-capture />');