From 5c1fdff691b9367d73f72f6a0298cb6a6e259f35 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Tue, 11 Nov 2014 11:20:39 +0200 Subject: [PATCH] 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 --- src/ng/directive/input.js | 2 +- test/ng/directive/inputSpec.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index d96541c7..69fa1401 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -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); }; } }; diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 59423ea2..043521a5 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -2495,6 +2495,16 @@ describe('input', function() { expect(inputElm).toBeValid(); }); + it('should accept values of any length when maxlength is negative', function() { + compileInput(''); + + changeInputValueTo(''); + expect(inputElm).toBeValid(); + + changeInputValueTo('aaaaaaaaaa'); + expect(inputElm).toBeValid(); + }); + it('should listen on ng-maxlength when maxlength is observed', function() { var value = 0; compileInput('');