Skip to content

Commit d63354a

Browse files
committed
feat(input): make expressions work for ng-Maxlenth and ng-Minlength
ng-minlength and ng-maxlength attributes do not accept expressions as input parameter. Make it work as intended. Fix based on angular#1077. Closes angular#1405
1 parent c6b4ab3 commit d63354a

File tree

2 files changed

+73
-44
lines changed

2 files changed

+73
-44
lines changed

src/ng/directive/input.js

100644100755
+33-42
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ var inputType = {
1616
* @param {string} ngModel Assignable angular expression to data-bind to.
1717
* @param {string=} name Property name of the form under which the control is published.
1818
* @param {string=} required Sets `required` validation error key if the value is not entered.
19-
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
19+
* @param {expression=} ngMinlength Sets `minlength` validation error key if the value is shorter than
2020
* minlength.
21-
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
21+
* @param {expression=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
2222
* maxlength.
2323
* @param {string=} ngPattern Sets `pattern` validation error key if the value does not match the
2424
* RegExp pattern expression. Expected value is `/regexp/` for inline patterns or `regexp` for
@@ -93,9 +93,9 @@ var inputType = {
9393
* @param {string=} min Sets the `min` validation error key if the value entered is less then `min`.
9494
* @param {string=} max Sets the `max` validation error key if the value entered is greater then `min`.
9595
* @param {string=} required Sets `required` validation error key if the value is not entered.
96-
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
96+
* @param {expression=} ngMinlength Sets `minlength` validation error key if the value is shorter than
9797
* minlength.
98-
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
98+
* @param {expression=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
9999
* maxlength.
100100
* @param {string=} ngPattern Sets `pattern` validation error key if the value does not match the
101101
* RegExp pattern expression. Expected value is `/regexp/` for inline patterns or `regexp` for
@@ -159,9 +159,9 @@ var inputType = {
159159
* @param {string} ngModel Assignable angular expression to data-bind to.
160160
* @param {string=} name Property name of the form under which the control is published.
161161
* @param {string=} required Sets `required` validation error key if the value is not entered.
162-
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
162+
* @param {expression=} ngMinlength Sets `minlength` validation error key if the value is shorter than
163163
* minlength.
164-
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
164+
* @param {expression=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
165165
* maxlength.
166166
* @param {string=} ngPattern Sets `pattern` validation error key if the value does not match the
167167
* RegExp pattern expression. Expected value is `/regexp/` for inline patterns or `regexp` for
@@ -224,9 +224,9 @@ var inputType = {
224224
* @param {string} ngModel Assignable angular expression to data-bind to.
225225
* @param {string=} name Property name of the form under which the control is published.
226226
* @param {string=} required Sets `required` validation error key if the value is not entered.
227-
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
227+
* @param {expression=} ngMinlength Sets `minlength` validation error key if the value is shorter than
228228
* minlength.
229-
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
229+
* @param {expression=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
230230
* maxlength.
231231
* @param {string=} ngPattern Sets `pattern` validation error key if the value does not match the
232232
* RegExp pattern expression. Expected value is `/regexp/` for inline patterns or `regexp` for
@@ -460,39 +460,30 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
460460
ctrl.$parsers.push(patternValidator);
461461
}
462462

463-
// min length validator
464-
if (attr.ngMinlength) {
465-
var minlength = int(attr.ngMinlength);
466-
var minLengthValidator = function(value) {
467-
if (!isEmpty(value) && value.length < minlength) {
468-
ctrl.$setValidity('minlength', false);
469-
return undefined;
470-
} else {
471-
ctrl.$setValidity('minlength', true);
472-
return value;
473-
}
474-
};
475-
476-
ctrl.$parsers.push(minLengthValidator);
477-
ctrl.$formatters.push(minLengthValidator);
478-
}
463+
function applyMinMaxLength(minOrMax, langthInvalid) {
464+
if (attr.hasOwnProperty(minOrMax)) {
465+
var validityValue = minOrMax === 'ngMinlength' ? 'minlength' : 'maxlength';
466+
var validator = function(value) {
467+
var parsedValue = parseFloat(attr[minOrMax], 10);
468+
if (!isEmpty(value) && langthInvalid(value, parsedValue)) {
469+
ctrl.$setValidity(validityValue, false);
470+
return undefined;
471+
} else {
472+
ctrl.$setValidity(validityValue, true);
473+
return value;
474+
}
475+
};
479476

480-
// max length validator
481-
if (attr.ngMaxlength) {
482-
var maxlength = int(attr.ngMaxlength);
483-
var maxLengthValidator = function(value) {
484-
if (!isEmpty(value) && value.length > maxlength) {
485-
ctrl.$setValidity('maxlength', false);
486-
return undefined;
487-
} else {
488-
ctrl.$setValidity('maxlength', true);
489-
return value;
490-
}
491-
};
477+
ctrl.$parsers.push(validator);
478+
ctrl.$formatters.push(validator);
492479

493-
ctrl.$parsers.push(maxLengthValidator);
494-
ctrl.$formatters.push(maxLengthValidator);
480+
attr.$observe(minOrMax, function() {
481+
ctrl.$setViewValue(ctrl.$viewValue);
482+
});
483+
}
495484
}
485+
applyMinMaxLength('ngMinlength', function langthInvalid(value, min) {return value.length < min;});
486+
applyMinMaxLength('ngMaxlength', function langthInvalid(value, max) {return value.length > max;});
496487
}
497488

498489
function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
@@ -653,9 +644,9 @@ function checkboxInputType(scope, element, attr, ctrl) {
653644
* @param {string} ngModel Assignable angular expression to data-bind to.
654645
* @param {string=} name Property name of the form under which the control is published.
655646
* @param {string=} required Sets `required` validation error key if the value is not entered.
656-
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
647+
* @param {expression=} ngMinlength Sets `minlength` validation error key if the value is shorter than
657648
* minlength.
658-
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
649+
* @param {expression=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
659650
* maxlength.
660651
* @param {string=} ngPattern Sets `pattern` validation error key if the value does not match the
661652
* RegExp pattern expression. Expected value is `/regexp/` for inline patterns or `regexp` for
@@ -677,9 +668,9 @@ function checkboxInputType(scope, element, attr, ctrl) {
677668
* @param {string} ngModel Assignable angular expression to data-bind to.
678669
* @param {string=} name Property name of the form under which the control is published.
679670
* @param {string=} required Sets `required` validation error key if the value is not entered.
680-
* @param {number=} ngMinlength Sets `minlength` validation error key if the value is shorter than
671+
* @param {expression=} ngMinlength Sets `minlength` validation error key if the value is shorter than
681672
* minlength.
682-
* @param {number=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
673+
* @param {expression=} ngMaxlength Sets `maxlength` validation error key if the value is longer than
683674
* maxlength.
684675
* @param {string=} ngPattern Sets `pattern` validation error key if the value does not match the
685676
* RegExp pattern expression. Expected value is `/regexp/` for inline patterns or `regexp` for

test/ng/directive/inputSpec.js

100644100755
+40-2
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ describe('input', function() {
505505

506506
describe('minlength', function() {
507507

508-
it('should invalid shorter than given minlenght', function() {
508+
it('should invalid shorter than given minlength', function() {
509509
compileInput('<input type="text" ng-model="value" ng-minlength="3" />');
510510

511511
changeInputValueTo('aa');
@@ -514,12 +514,31 @@ describe('input', function() {
514514
changeInputValueTo('aaa');
515515
expect(scope.value).toBe('aaa');
516516
});
517+
518+
519+
it('should invalid shorter than given minlength-expressions', function() {
520+
scope.$apply(function(){
521+
scope.min = 5
522+
});
523+
compileInput('<input type="text" ng-model="value" ng-minlength="{{min}}" />');
524+
changeInputValueTo('aa');
525+
scope.$digest();
526+
expect(scope.value).toBeUndefined();
527+
528+
changeInputValueTo('aaaaa');
529+
expect(scope.value).toBe('aaaaa');
530+
531+
scope.$apply(function(){
532+
scope.min = 6;
533+
});
534+
expect(scope.value).toBeUndefined();
535+
});
517536
});
518537

519538

520539
describe('maxlength', function() {
521540

522-
it('should invalid shorter than given maxlenght', function() {
541+
it('should invalid longer than given maxlength', function() {
523542
compileInput('<input type="text" ng-model="value" ng-maxlength="5" />');
524543

525544
changeInputValueTo('aaaaaaaa');
@@ -528,6 +547,25 @@ describe('input', function() {
528547
changeInputValueTo('aaa');
529548
expect(scope.value).toBe('aaa');
530549
});
550+
551+
552+
it('should invalid longer than given maxlength-expressions', function() {
553+
scope.$apply(function(){
554+
scope.max = 5
555+
});
556+
compileInput('<input type="text" ng-model="value" ng-maxlength="{{max}}" />');
557+
changeInputValueTo('aaaaaaaa');
558+
scope.$digest();
559+
expect(scope.value).toBeUndefined();
560+
561+
changeInputValueTo('aaa');
562+
expect(scope.value).toBe('aaa');
563+
564+
scope.$apply(function(){
565+
scope.max = 2;
566+
});
567+
expect(scope.value).toBeUndefined();
568+
});
531569
});
532570

533571

0 commit comments

Comments
 (0)