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

Commit 86d373f

Browse files
committed
fix: fix validation when min < max and val > max, fix tests in browsers without support
1 parent 0dbe342 commit 86d373f

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

src/ng/directive/input.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15291529
}
15301530

15311531
// TODO(matsko): implement validateLater to reduce number of validations
1532-
if (minAttrType === 'min') {
1532+
if (supportsRange && minAttrType === 'min') {
15331533
var elVal = element.val();
15341534
// IE11 doesn't set the el val correctly if the maxVal is less than the element value
15351535
if (minVal > elVal) {
@@ -1543,9 +1543,11 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15431543

15441544
var minAttrType = isDefined(attr.min) ? 'min' : attr.ngMin ? 'ngMin' : false;
15451545
if (minAttrType) {
1546-
ctrl.$validators.min = isDefined(attr.min) && supportsRange && isDefined(validity.rangeUnderflow) ?
1547-
function nativeMinValidator(value) {
1548-
return !element[0].validity.rangeUnderflow;
1546+
// Since ngMin doesn't set the min attr, the browser doesn't adjust the input value as setting min would
1547+
ctrl.$validators.min = isDefined(attr.min) && supportsRange ?
1548+
function noopMinValidator(value) {
1549+
// Since all browsers set the input to a valid value, we don't need to check validity
1550+
return true;
15491551
} :
15501552
function minValidator(modelValue, viewValue) {
15511553
return ctrl.$isEmpty(viewValue) || isUndefined(minVal) || viewValue >= minVal;
@@ -1567,7 +1569,7 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15671569
return;
15681570
}
15691571

1570-
if (maxAttrType === 'max') {
1572+
if (supportsRange && maxAttrType === 'max') {
15711573
var elVal = element.val();
15721574
// IE11 doesn't set the el val correctly if the maxVal is less than the element value
15731575
if (maxVal < elVal) {
@@ -1581,10 +1583,10 @@ function rangeInputType(scope, element, attr, ctrl, $sniffer, $browser) {
15811583
var maxAttrType = isDefined(attr.max) ? 'max' : attr.ngMax ? 'ngMax' : false;
15821584
if (maxAttrType) {
15831585
// Since ngMax doesn't set the max attr, the browser doesn't adjust the input value as setting max would
1584-
ctrl.$validators.max = isDefined(attr.max) && supportsRange && isDefined(validity.rangeOverflow) ?
1585-
function nativeMaxValidator() {
1586-
// IE10+11 incorrectly set the rangeOverflow error when max < min
1587-
return (msie > 9 && maxVal < minVal) ? true : !element[0].validity.rangeOverflow;
1586+
ctrl.$validators.max = isDefined(attr.max) && supportsRange ?
1587+
function noopMaxValidator() {
1588+
// Since all browsers set the input to a valid value, we don't need to check validity
1589+
return true;
15881590
} :
15891591
function maxValidator(modelValue, viewValue) {
15901592
return ctrl.$isEmpty(viewValue) || isUndefined(maxVal) || viewValue <= maxVal;

test/ng/directive/inputSpec.js

+36-2
Original file line numberDiff line numberDiff line change
@@ -2981,6 +2981,40 @@ describe('input', function() {
29812981
expect(scope.value).toBe(100);
29822982
expect(scope.form.alias.$error.min).toBeFalsy();
29832983
});
2984+
2985+
it('should validate even if the min value changes on-the-fly', function() {
2986+
scope.min = 10;
2987+
var inputElm = helper.compileInput('<input type="range" ng-model="value" name="alias" min="{{min}}" />');
2988+
2989+
helper.changeInputValueTo('15');
2990+
expect(inputElm).toBeValid();
2991+
expect(scope.value).toBe(15);
2992+
2993+
scope.min = 20;
2994+
scope.$digest();
2995+
expect(inputElm).toBeInvalid();
2996+
expect(scope.value).toBeUndefined();
2997+
expect(inputElm.val()).toBe('15');
2998+
2999+
scope.min = null;
3000+
scope.$digest();
3001+
expect(inputElm).toBeValid();
3002+
expect(scope.value).toBe(15);
3003+
expect(inputElm.val()).toBe('15');
3004+
3005+
scope.min = '16';
3006+
scope.$digest();
3007+
expect(inputElm).toBeInvalid();
3008+
expect(scope.value).toBeUndefined();
3009+
expect(inputElm.val()).toBe('15');
3010+
3011+
scope.min = 'abc';
3012+
scope.$digest();
3013+
expect(inputElm).toBeValid();
3014+
expect(scope.value).toBe(15);
3015+
expect(inputElm.val()).toBe('15');
3016+
});
3017+
29843018
}
29853019
});
29863020

@@ -3111,7 +3145,7 @@ describe('input', function() {
31113145
scope.max = 0;
31123146
scope.$digest();
31133147
expect(inputElm).toBeInvalid();
3114-
expect(scope.value).toBe(5);
3148+
expect(scope.value).toBeUndefined();
31153149
expect(inputElm.val()).toBe('5');
31163150

31173151
scope.max = null;
@@ -3123,7 +3157,7 @@ describe('input', function() {
31233157
scope.max = '4';
31243158
scope.$digest();
31253159
expect(inputElm).toBeInvalid();
3126-
expect(scope.value).toBe(5);
3160+
expect(scope.value).toBeUndefined();
31273161
expect(inputElm.val()).toBe('5');
31283162

31293163
scope.max = 'abc';

0 commit comments

Comments
 (0)