Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 77ce5b8

Browse files
committedAug 29, 2014
fix(input): validate minlength/maxlength for non-string values
Use the viewValue rather than modelValue when validating. The viewValue should always be a string, and should reflect what the user has entered, or the formatted model value. BREAKING CHANGE: Always uses the viewValue when validating minlength and maxlength. Closes #7967 Closes #8811
1 parent c5f1ca3 commit 77ce5b8

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed
 

‎src/ng/directive/input.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2473,8 +2473,8 @@ 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+
return ctrl.$isEmpty(viewValue) || viewValue.length <= maxlength;
24782478
};
24792479
}
24802480
};
@@ -2492,8 +2492,8 @@ var minlengthDirective = function() {
24922492
minlength = int(value) || 0;
24932493
ctrl.$validate();
24942494
});
2495-
ctrl.$validators.minlength = function(value) {
2496-
return ctrl.$isEmpty(value) || value.length >= minlength;
2495+
ctrl.$validators.minlength = function(modelValue, viewValue) {
2496+
return ctrl.$isEmpty(viewValue) || viewValue.length >= minlength;
24972497
};
24982498
}
24992499
};

‎test/ng/directive/inputSpec.js

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

29062997
describe('email', function() {

0 commit comments

Comments
 (0)
This repository has been archived.