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

feat(ngMaxlength): disable maxlength when not set to a non-negative integer #9998

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var inputType = {
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
* minlength.
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
* maxlength.
* maxlength. Setting the attribute to a negative or non-numeric value, allows view values of
* any length.
* @param {string=} pattern Similar to `ngPattern` except that the attribute value is the actual string
* that contains the regular expression body that will be converted to a regular expression
* as in the ngPattern directive.
Expand Down Expand Up @@ -589,7 +590,8 @@ var inputType = {
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
* minlength.
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
* maxlength.
* maxlength. Setting the attribute to a negative or non-numeric value, allows view values of
* any length.
* @param {string=} pattern Similar to `ngPattern` except that the attribute value is the actual string
* that contains the regular expression body that will be converted to a regular expression
* as in the ngPattern directive.
Expand Down Expand Up @@ -676,7 +678,8 @@ var inputType = {
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
* minlength.
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
* maxlength.
* maxlength. Setting the attribute to a negative or non-numeric value, allows view values of
* any length.
* @param {string=} pattern Similar to `ngPattern` except that the attribute value is the actual string
* that contains the regular expression body that will be converted to a regular expression
* as in the ngPattern directive.
Expand Down Expand Up @@ -764,7 +767,8 @@ var inputType = {
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
* minlength.
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
* maxlength.
* maxlength. Setting the attribute to a negative or non-numeric value, allows view values of
* any length.
* @param {string=} pattern Similar to `ngPattern` except that the attribute value is the actual string
* that contains the regular expression body that will be converted to a regular expression
* as in the ngPattern directive.
Expand Down Expand Up @@ -1370,7 +1374,8 @@ function checkboxInputType(scope, element, attr, ctrl, $sniffer, $browser, $filt
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
* minlength.
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
* maxlength.
* maxlength. Setting the attribute to a negative or non-numeric value, allows view values of any
* length.
* @param {string=} ngPattern Sets `pattern` validation error key if the value does not match the
* RegExp pattern expression. Expected value is `/regexp/` for inline patterns or `regexp` for
* patterns defined as scope expressions.
Expand Down Expand Up @@ -1402,7 +1407,8 @@ function checkboxInputType(scope, element, attr, ctrl, $sniffer, $browser, $filt
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
* minlength.
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
* maxlength.
* maxlength. Setting the attribute to a negative or non-numeric value, allows view values of any
* length.
* @param {string=} ngPattern Sets `pattern` validation error key if the value does not match the
* RegExp pattern expression. Expected value is `/regexp/` for inline patterns or `regexp` for
* patterns defined as scope expressions.
Expand Down Expand Up @@ -2690,13 +2696,14 @@ var maxlengthDirective = function() {
link: function(scope, elm, attr, ctrl) {
if (!ctrl) return;

var maxlength = 0;
var maxlength = -1;
attr.$observe('maxlength', function(value) {
maxlength = int(value) || 0;
var intVal = int(value);
maxlength = isNaN(intVal) ? -1 : intVal;
ctrl.$validate();
});
ctrl.$validators.maxlength = function(modelValue, viewValue) {
return ctrl.$isEmpty(modelValue) || viewValue.length <= maxlength;
return (maxlength < 0) || ctrl.$isEmpty(modelValue) || (viewValue.length <= maxlength);
};
}
};
Expand Down
41 changes: 41 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2495,6 +2495,47 @@ describe('input', function() {
expect(inputElm).toBeValid();
});

it('should only accept empty values when maxlength is 0', function() {
compileInput('<input type="text" ng-model="value" ng-maxlength="0" />');

changeInputValueTo('');
expect(inputElm).toBeValid();

changeInputValueTo('a');
expect(inputElm).toBeInvalid();
});

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 accept values of any length when maxlength is non-numeric', function() {
compileInput('<input type="text" ng-model="value" ng-maxlength="{{maxlength}}" />');
changeInputValueTo('aaaaaaaaaa');

scope.$apply('maxlength = "5"');
expect(inputElm).toBeInvalid();

scope.$apply('maxlength = "abc"');
expect(inputElm).toBeValid();

scope.$apply('maxlength = ""');
expect(inputElm).toBeValid();

scope.$apply('maxlength = null');
expect(inputElm).toBeValid();

scope.someObj = {};
scope.$apply('maxlength = someObj');
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 />');
Expand Down