From 9bcb819fd7b7b5fe1840479e5a234501faf4ae54 Mon Sep 17 00:00:00 2001 From: Konstantin Stepanov Date: Tue, 18 Oct 2011 02:18:00 +0300 Subject: [PATCH 1/5] (feature)validation support for minlength and maxlength --- src/widget/input.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/widget/input.js b/src/widget/input.js index e920733be8ae..fc35c92f01fc 100644 --- a/src/widget/input.js +++ b/src/widget/input.js @@ -79,6 +79,8 @@ var INTEGER_REGEXP = /^\s*(\-|\+)?\d+\s*$/; * @param {string} ng:model Assignable angular expression to data-bind to. * @param {string=} name Property name of the form under which the widgets is published. * @param {string=} required Sets `REQUIRED` validation error key if the value is not entered. + * @param {number=} minlength Sets `MINLENGTH` validation error key if the value is shorter than minlength. + * @param {number=} maxlength Sets `MAXLENGTH` validation error key if the value is longer than maxlength. * @param {string=} ng:pattern 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. @@ -656,6 +658,8 @@ angularWidget('input', function(inputElement){ modelScope = this, patternMatch, widget, pattern = trim(inputElement.attr('ng:pattern')), + minlength = parseInt(inputElement.attr('minlength'), 10), + maxlength = parseInt(inputElement.attr('maxlength'), 10), loadFromScope = type.match(/^\s*\@\s*(.*)/); @@ -713,6 +717,8 @@ angularWidget('input', function(inputElement){ widget.$on('$validate', function(event) { var $viewValue = trim(widget.$viewValue); var inValid = widget.$required && !$viewValue; + var tooLong = maxlength && $viewValue && $viewValue.length > maxlength, + tooShort = minlength && $viewValue && $viewValue.length < minlength; var missMatch = $viewValue && !patternMatch($viewValue); if (widget.$error.REQUIRED != inValid){ widget.$emit(inValid ? '$invalid' : '$valid', 'REQUIRED'); @@ -720,6 +726,12 @@ angularWidget('input', function(inputElement){ if (widget.$error.PATTERN != missMatch){ widget.$emit(missMatch ? '$invalid' : '$valid', 'PATTERN'); } + if (widget.$error.MINLENGTH != tooShort){ + widget.$emit(tooShort ? '$invalid' : '$valid', 'MINLENGTH'); + } + if (widget.$error.MAXLENGTH != tooLong){ + widget.$emit(tooLong ? '$invalid' : '$valid', 'MAXLENGTH'); + } }); forEach(['valid', 'invalid', 'pristine', 'dirty'], function(name) { From 77342e947ca03048ca6b42fa0af22e1c4f2dba20 Mon Sep 17 00:00:00 2001 From: Konstantin Stepanov Date: Wed, 19 Oct 2011 14:26:34 +0300 Subject: [PATCH 2/5] (test) password field type is not changed with text type --- test/widget/inputSpec.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/widget/inputSpec.js b/test/widget/inputSpec.js index 837d8c83190c..edd7eb5a2349 100644 --- a/test/widget/inputSpec.js +++ b/test/widget/inputSpec.js @@ -412,6 +412,13 @@ describe('widget: input', function() { }); }); + describe('password', function () { + it('should not change password type to text', function () { + doc = angular.element('
'); + scope = angular.compile(doc)(); + expect(doc.find('input')[0].getAttribute('type')).toEqual('password'); + }); + }); it('should ignore text widget which have no name', function() { compile(''); From 4a0c71be1e1789e31a6d1557cc80ee52d61b82b9 Mon Sep 17 00:00:00 2001 From: Konstantin Stepanov Date: Wed, 19 Oct 2011 19:44:41 +0300 Subject: [PATCH 3/5] {min|max}length -> ng:{min|max}length --- src/widget/input.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/widget/input.js b/src/widget/input.js index fc35c92f01fc..87564fd04fbc 100644 --- a/src/widget/input.js +++ b/src/widget/input.js @@ -79,8 +79,8 @@ var INTEGER_REGEXP = /^\s*(\-|\+)?\d+\s*$/; * @param {string} ng:model Assignable angular expression to data-bind to. * @param {string=} name Property name of the form under which the widgets is published. * @param {string=} required Sets `REQUIRED` validation error key if the value is not entered. - * @param {number=} minlength Sets `MINLENGTH` validation error key if the value is shorter than minlength. - * @param {number=} maxlength Sets `MAXLENGTH` validation error key if the value is longer than maxlength. + * @param {number=} ng:minlength Sets `MINLENGTH` validation error key if the value is shorter than minlength. + * @param {number=} ng:maxlength Sets `MAXLENGTH` validation error key if the value is longer than maxlength. * @param {string=} ng:pattern 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. @@ -658,8 +658,8 @@ angularWidget('input', function(inputElement){ modelScope = this, patternMatch, widget, pattern = trim(inputElement.attr('ng:pattern')), - minlength = parseInt(inputElement.attr('minlength'), 10), - maxlength = parseInt(inputElement.attr('maxlength'), 10), + minlength = parseInt(inputElement.attr('ng:minlength'), 10), + maxlength = parseInt(inputElement.attr('ng:maxlength'), 10), loadFromScope = type.match(/^\s*\@\s*(.*)/); From 324b062653a2185022abd96026e8c1c600ae79a8 Mon Sep 17 00:00:00 2001 From: Konstantin Stepanov Date: Wed, 19 Oct 2011 20:02:34 +0300 Subject: [PATCH 4/5] (test) ng:minlength and ng:maxlength --- test/widget/inputSpec.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/widget/inputSpec.js b/test/widget/inputSpec.js index edd7eb5a2349..8b17108e153c 100644 --- a/test/widget/inputSpec.js +++ b/test/widget/inputSpec.js @@ -556,6 +556,10 @@ describe('widget: input', function() { scope.regexp = /^\d\d\d-\d\d-\d\d\d\d$/; }); + itShouldVerify('text with length limits', + ['aaa', 'aaaaa', 'aaaaaaaaa'], + ['', 'a', 'aa', 'aaaaaaaaaa'], + {'ng:minlength': 3, 'ng:maxlength': 9}); it('should throw an error when scope pattern can\'t be found', function() { var el = jqLite(''), From 598f5f935fcfe2863309f238e0423f4b7ffea054 Mon Sep 17 00:00:00 2001 From: Konstantin Stepanov Date: Wed, 19 Oct 2011 20:35:29 +0300 Subject: [PATCH 5/5] (bugfix) one trailing char was cut from pattern --- src/widget/input.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/widget/input.js b/src/widget/input.js index 87564fd04fbc..0ff15d640455 100644 --- a/src/widget/input.js +++ b/src/widget/input.js @@ -667,7 +667,7 @@ angularWidget('input', function(inputElement){ patternMatch = valueFn(true); } else { if (pattern.match(/^\/(.*)\/$/)) { - pattern = new RegExp(pattern.substring(1, pattern.length - 2)); + pattern = new RegExp(pattern.substring(1, pattern.length - 1)); patternMatch = function(value) { return pattern.test(value); }