Skip to content

Commit 7faa60c

Browse files
committed
fix(input): validate minlength/maxlength for non-string values
Use viewValue for minlength/maxlength validation if model value is not a string. This allows ngMinlength and ngMaxlength to be used for number inputs. Closes angular#7967 Closes angular#8811
1 parent 97a1b39 commit 7faa60c

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

src/ng/directive/input.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -2473,8 +2473,11 @@ var maxlengthDirective = function() {
24732473
maxlength = int(value) || 0;
24742474
ctrl.$validate();
24752475
});
2476-
ctrl.$validators.maxlength = function(value) {
2477-
return ctrl.$isEmpty(value) || value.length <= maxlength;
2476+
ctrl.$validators.maxlength = function(modelValue, viewValue) {
2477+
if (!isString(modelValue)) {
2478+
modelValue = viewValue;
2479+
}
2480+
return ctrl.$isEmpty(modelValue) || modelValue.length <= maxlength;
24782481
};
24792482
}
24802483
};
@@ -2492,8 +2495,11 @@ var minlengthDirective = function() {
24922495
minlength = int(value) || 0;
24932496
ctrl.$validate();
24942497
});
2495-
ctrl.$validators.minlength = function(value) {
2496-
return ctrl.$isEmpty(value) || value.length >= minlength;
2498+
ctrl.$validators.minlength = function(modelValue, viewValue) {
2499+
if (!isString(modelValue)) {
2500+
modelValue = viewValue;
2501+
}
2502+
return ctrl.$isEmpty(modelValue) || modelValue.length >= minlength;
24972503
};
24982504
}
24992505
};

test/ng/directive/inputSpec.js

+91
Original file line numberDiff line numberDiff line change
@@ -2909,6 +2909,97 @@ describe('input', function() {
29092909
expect(scope.form.alias.$error.required).toBeTruthy();
29102910
});
29112911
});
2912+
2913+
describe('minlength', function() {
2914+
2915+
it('should invalidate values that are shorter than the given minlength', function() {
2916+
compileInput('<input type="number" ng-model="value" ng-minlength="3" />');
2917+
2918+
changeInputValueTo('12');
2919+
expect(inputElm).toBeInvalid();
2920+
2921+
changeInputValueTo('123');
2922+
expect(inputElm).toBeValid();
2923+
});
2924+
2925+
it('should listen on ng-minlength when minlength is observed', function() {
2926+
var value = 0;
2927+
compileInput('<input type="number" ng-model="value" ng-minlength="min" attr-capture />');
2928+
attrs.$observe('minlength', function(v) {
2929+
value = int(attrs.minlength);
2930+
});
2931+
2932+
scope.$apply(function() {
2933+
scope.min = 5;
2934+
});
2935+
2936+
expect(value).toBe(5);
2937+
});
2938+
2939+
it('should observe the standard minlength attribute and register it as a validator on the model', function() {
2940+
compileInput('<input type="number" name="input" ng-model="value" minlength="{{ min }}" />');
2941+
scope.$apply(function() {
2942+
scope.min = 10;
2943+
});
2944+
2945+
changeInputValueTo('12345');
2946+
expect(inputElm).toBeInvalid();
2947+
expect(scope.form.input.$error.minlength).toBe(true);
2948+
2949+
scope.$apply(function() {
2950+
scope.min = 5;
2951+
});
2952+
2953+
expect(inputElm).toBeValid();
2954+
expect(scope.form.input.$error.minlength).not.toBe(true);
2955+
});
2956+
});
2957+
2958+
2959+
describe('maxlength', function() {
2960+
2961+
it('should invalidate values that are longer than the given maxlength', function() {
2962+
compileInput('<input type="number" ng-model="value" ng-maxlength="5" />');
2963+
2964+
changeInputValueTo('12345678');
2965+
expect(inputElm).toBeInvalid();
2966+
2967+
changeInputValueTo('123');
2968+
expect(inputElm).toBeValid();
2969+
});
2970+
2971+
it('should listen on ng-maxlength when maxlength is observed', function() {
2972+
var value = 0;
2973+
compileInput('<input type="number" ng-model="value" ng-maxlength="max" attr-capture />');
2974+
attrs.$observe('maxlength', function(v) {
2975+
value = int(attrs.maxlength);
2976+
});
2977+
2978+
scope.$apply(function() {
2979+
scope.max = 10;
2980+
});
2981+
2982+
expect(value).toBe(10);
2983+
});
2984+
2985+
it('should observe the standard maxlength attribute and register it as a validator on the model', function() {
2986+
compileInput('<input type="number" name="input" ng-model="value" maxlength="{{ max }}" />');
2987+
scope.$apply(function() {
2988+
scope.max = 1;
2989+
});
2990+
2991+
changeInputValueTo('12345');
2992+
expect(inputElm).toBeInvalid();
2993+
expect(scope.form.input.$error.maxlength).toBe(true);
2994+
2995+
scope.$apply(function() {
2996+
scope.max = 6;
2997+
});
2998+
2999+
expect(inputElm).toBeValid();
3000+
expect(scope.form.input.$error.maxlength).not.toBe(true);
3001+
});
3002+
});
29123003
});
29133004

29143005
describe('email', function() {

0 commit comments

Comments
 (0)