Skip to content

Commit 0a3ddfc

Browse files
committed
fix(input[number]): validate min/max against viewValue
This brings the validation in line with HTML5 validation and all other validators. Fixes angular#12761 BREAKING CHANGE ...
1 parent 181ac0b commit 0a3ddfc

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

src/ng/directive/input.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1604,8 +1604,8 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16041604
var maxVal;
16051605

16061606
if (isDefined(attr.min) || attr.ngMin) {
1607-
ctrl.$validators.min = function(value) {
1608-
return ctrl.$isEmpty(value) || isUndefined(minVal) || value >= minVal;
1607+
ctrl.$validators.min = function(modelValue, viewValue) {
1608+
return ctrl.$isEmpty(viewValue) || isUndefined(minVal) || viewValue >= minVal;
16091609
};
16101610

16111611
attr.$observe('min', function(val) {
@@ -1616,8 +1616,8 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
16161616
}
16171617

16181618
if (isDefined(attr.max) || attr.ngMax) {
1619-
ctrl.$validators.max = function(value) {
1620-
return ctrl.$isEmpty(value) || isUndefined(maxVal) || value <= maxVal;
1619+
ctrl.$validators.max = function(modelValue, viewValue) {
1620+
return ctrl.$isEmpty(viewValue) || isUndefined(maxVal) || viewValue <= maxVal;
16211621
};
16221622

16231623
attr.$observe('max', function(val) {

test/ng/directive/inputSpec.js

+62
Original file line numberDiff line numberDiff line change
@@ -2465,6 +2465,21 @@ describe('input', function() {
24652465
expect($rootScope.form.alias.$error.min).toBeFalsy();
24662466
});
24672467

2468+
2469+
it('should validate against the viewValue', function() {
2470+
var inputElm = helper.compileInput('<input type="number" num-parse ng-model="value" name="alias" min="10" />');
2471+
var ngModelCtrl = inputElm.controller('ngModel');
2472+
ngModelCtrl.$parsers.push(function(value) {
2473+
return value - 5;
2474+
});
2475+
2476+
helper.changeInputValueTo('10');
2477+
expect(inputElm).toBeValid();
2478+
expect($rootScope.value).toBe(5);
2479+
expect($rootScope.form.alias.$error.min).toBeFalsy();
2480+
});
2481+
2482+
24682483
it('should validate even if min value changes on-the-fly', function() {
24692484
$rootScope.min = undefined;
24702485
var inputElm = helper.compileInput('<input type="number" ng-model="value" name="alias" min="{{min}}" />');
@@ -2511,6 +2526,21 @@ describe('input', function() {
25112526
expect($rootScope.form.alias.$error.min).toBeFalsy();
25122527
});
25132528

2529+
2530+
it('should validate against the viewValue', function() {
2531+
var inputElm = helper.compileInput('<input type="number" num-parse ng-model="value" name="alias" ng-min="10" />');
2532+
var ngModelCtrl = inputElm.controller('ngModel');
2533+
ngModelCtrl.$parsers.push(function(value) {
2534+
return value - 5;
2535+
});
2536+
2537+
helper.changeInputValueTo('10');
2538+
expect(inputElm).toBeValid();
2539+
expect($rootScope.value).toBe(5);
2540+
expect($rootScope.form.alias.$error.min).toBeFalsy();
2541+
});
2542+
2543+
25142544
it('should validate even if the ngMin value changes on-the-fly', function() {
25152545
$rootScope.min = undefined;
25162546
var inputElm = helper.compileInput('<input type="number" ng-model="value" name="alias" ng-min="min" />');
@@ -2558,6 +2588,22 @@ describe('input', function() {
25582588
expect($rootScope.form.alias.$error.max).toBeFalsy();
25592589
});
25602590

2591+
2592+
it('should validate against the viewValue', function() {
2593+
var inputElm = helper.compileInput('<input type="number"' +
2594+
'num-parse ng-model="value" name="alias" max="10" />');
2595+
var ngModelCtrl = inputElm.controller('ngModel');
2596+
ngModelCtrl.$parsers.push(function(value) {
2597+
return value + 5;
2598+
});
2599+
2600+
helper.changeInputValueTo('9');
2601+
expect(inputElm).toBeValid();
2602+
expect($rootScope.value).toBe(14);
2603+
expect($rootScope.form.alias.$error.max).toBeFalsy();
2604+
});
2605+
2606+
25612607
it('should validate even if max value changes on-the-fly', function() {
25622608
$rootScope.max = undefined;
25632609
var inputElm = helper.compileInput('<input type="number" ng-model="value" name="alias" max="{{max}}" />');
@@ -2604,6 +2650,22 @@ describe('input', function() {
26042650
expect($rootScope.form.alias.$error.max).toBeFalsy();
26052651
});
26062652

2653+
2654+
it('should validate against the viewValue', function() {
2655+
var inputElm = helper.compileInput('<input type="number"' +
2656+
'num-parse ng-model="value" name="alias" ng-max="10" />');
2657+
var ngModelCtrl = inputElm.controller('ngModel');
2658+
ngModelCtrl.$parsers.push(function(value) {
2659+
return value + 5;
2660+
});
2661+
2662+
helper.changeInputValueTo('9');
2663+
expect(inputElm).toBeValid();
2664+
expect($rootScope.value).toBe(14);
2665+
expect($rootScope.form.alias.$error.max).toBeFalsy();
2666+
});
2667+
2668+
26072669
it('should validate even if the ngMax value changes on-the-fly', function() {
26082670
$rootScope.max = undefined;
26092671
var inputElm = helper.compileInput('<input type="number" ng-model="value" name="alias" ng-max="max" />');

0 commit comments

Comments
 (0)