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

Commit 25541c1

Browse files
committed
fix(ngModel): consider ngMin/ngMax values when validating number input types
With this fix ngModel will treat ngMin as a min error and ngMax as a max error. This also means that when either of these two values is changed then ngModel will revaliate itself.
1 parent 7b273a2 commit 25541c1

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

src/jqLite.js

+2
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ forEach('input,select,option,textarea,button,form,details'.split(','), function(
506506
var ALIASED_ATTR = {
507507
'ngMinlength' : 'minlength',
508508
'ngMaxlength' : 'maxlength',
509+
'ngMin' : 'min',
510+
'ngMax' : 'max',
509511
'ngPattern' : 'pattern'
510512
};
511513

src/ng/directive/input.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
11281128
return value;
11291129
});
11301130

1131-
if (attr.min) {
1131+
if (attr.min || attr.ngMin) {
11321132
var minVal;
11331133
ctrl.$validators.min = function(value) {
11341134
return ctrl.$isEmpty(value) || isUndefined(minVal) || value >= minVal;
@@ -1144,7 +1144,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
11441144
});
11451145
}
11461146

1147-
if (attr.max) {
1147+
if (attr.max || attr.ngMax) {
11481148
var maxVal;
11491149
ctrl.$validators.max = function(value) {
11501150
return ctrl.$isEmpty(value) || isUndefined(maxVal) || value <= maxVal;

test/ng/directive/inputSpec.js

+82
Original file line numberDiff line numberDiff line change
@@ -2919,6 +2919,47 @@ describe('input', function() {
29192919
});
29202920
});
29212921

2922+
describe('ngMin', function() {
2923+
2924+
it('should validate', function() {
2925+
compileInput('<input type="number" ng-model="value" name="alias" ng-min="50" />');
2926+
2927+
changeInputValueTo('1');
2928+
expect(inputElm).toBeInvalid();
2929+
expect(scope.value).toBeFalsy();
2930+
expect(scope.form.alias.$error.min).toBeTruthy();
2931+
2932+
changeInputValueTo('100');
2933+
expect(inputElm).toBeValid();
2934+
expect(scope.value).toBe(100);
2935+
expect(scope.form.alias.$error.min).toBeFalsy();
2936+
});
2937+
2938+
it('should validate even if the ngMin value changes on-the-fly', function() {
2939+
scope.min = 10;
2940+
compileInput('<input type="number" ng-model="value" name="alias" ng-min="min" />');
2941+
2942+
changeInputValueTo('15');
2943+
expect(inputElm).toBeValid();
2944+
2945+
scope.min = 20;
2946+
scope.$digest();
2947+
expect(inputElm).toBeInvalid();
2948+
2949+
scope.min = null;
2950+
scope.$digest();
2951+
expect(inputElm).toBeValid();
2952+
2953+
scope.min = '20';
2954+
scope.$digest();
2955+
expect(inputElm).toBeInvalid();
2956+
2957+
scope.min = 'abc';
2958+
scope.$digest();
2959+
expect(inputElm).toBeValid();
2960+
});
2961+
});
2962+
29222963

29232964
describe('max', function() {
29242965

@@ -2961,6 +3002,47 @@ describe('input', function() {
29613002
});
29623003
});
29633004

3005+
describe('ngMax', function() {
3006+
3007+
it('should validate', function() {
3008+
compileInput('<input type="number" ng-model="value" name="alias" ng-max="5" />');
3009+
3010+
changeInputValueTo('20');
3011+
expect(inputElm).toBeInvalid();
3012+
expect(scope.value).toBeUndefined();
3013+
expect(scope.form.alias.$error.max).toBeTruthy();
3014+
3015+
changeInputValueTo('0');
3016+
expect(inputElm).toBeValid();
3017+
expect(scope.value).toBe(0);
3018+
expect(scope.form.alias.$error.max).toBeFalsy();
3019+
});
3020+
3021+
it('should validate even if the ngMax value changes on-the-fly', function() {
3022+
scope.max = 10;
3023+
compileInput('<input type="number" ng-model="value" name="alias" ng-max="max" />');
3024+
3025+
changeInputValueTo('5');
3026+
expect(inputElm).toBeValid();
3027+
3028+
scope.max = 0;
3029+
scope.$digest();
3030+
expect(inputElm).toBeInvalid();
3031+
3032+
scope.max = null;
3033+
scope.$digest();
3034+
expect(inputElm).toBeValid();
3035+
3036+
scope.max = '4';
3037+
scope.$digest();
3038+
expect(inputElm).toBeInvalid();
3039+
3040+
scope.max = 'abc';
3041+
scope.$digest();
3042+
expect(inputElm).toBeValid();
3043+
});
3044+
});
3045+
29643046

29653047
describe('required', function() {
29663048

0 commit comments

Comments
 (0)