diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index d96541c7f39c..9ddd15aa07b9 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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); }; } }; diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 59423ea29d69..f83f4ee58c90 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -2495,6 +2495,47 @@ describe('input', function() { expect(inputElm).toBeValid(); }); + it('should only accept empty values when maxlength is 0', function() { + compileInput(''); + + changeInputValueTo(''); + expect(inputElm).toBeValid(); + + changeInputValueTo('a'); + expect(inputElm).toBeInvalid(); + }); + + it('should accept values of any length when maxlength is negative', function() { + compileInput(''); + + changeInputValueTo(''); + expect(inputElm).toBeValid(); + + changeInputValueTo('aaaaaaaaaa'); + expect(inputElm).toBeValid(); + }); + + it('should accept values of any length when maxlength is non-numeric', function() { + compileInput(''); + 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('');