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

Commit 544001f

Browse files
committed
fix(input): ignore min/max if they are empty on all input types
When the min/max attributes are empty (i.e. `attrs.min/max === ''`), there should be no min/max validation applied (i.e. all values should be valid wrt min/max). This works correctly for `input[number]`, but not for date input family (`input[date/datetime-local/time/week/month]`). In the case on `input[number]`, an empty string for `attrs.min/max` is translated to `undefined` for `minVal/maxVal` and a check for `isUndefined(minVal/maxVal)` ensures that no min/max validation takes place. For the data input family, an empty string for `attrs.min/max` is translated to `NaN` (by `parseDate()`), so an additional check (for `isNaN(minVal/maxVal)`) is required. Fixes #12363 Closes #12785
1 parent 7175d0d commit 544001f

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

src/ng/directive/input.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ function createDateInputType(type, regexp, parseDate, format) {
13261326
}
13271327

13281328
function parseObservedDateValue(val) {
1329-
return isDefined(val) ? (isDate(val) ? val : parseDate(val)) : undefined;
1329+
return isDefined(val) && !isDate(val) ? parseDate(val) || undefined : val;
13301330
}
13311331
};
13321332
}

test/ng/directive/inputSpec.js

+88-3
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,14 @@ describe('input', function() {
688688
expect(inputElm).toBeInvalid();
689689
expect($rootScope.form.alias.$error.min).toBeTruthy();
690690
});
691+
692+
it('should validate if min is empty', function() {
693+
$rootScope.minVal = undefined;
694+
$rootScope.value = new Date(-9999, 0, 1, 0, 0, 0);
695+
$rootScope.$digest();
696+
697+
expect($rootScope.form.alias.$error.min).toBeFalsy();
698+
});
691699
});
692700

693701
describe('max', function() {
@@ -722,6 +730,14 @@ describe('input', function() {
722730
expect(inputElm).toBeInvalid();
723731
expect($rootScope.form.alias.$error.max).toBeTruthy();
724732
});
733+
734+
it('should validate if max is empty', function() {
735+
$rootScope.maxVal = undefined;
736+
$rootScope.value = new Date(9999, 11, 31, 23, 59, 59);
737+
$rootScope.$digest();
738+
739+
expect($rootScope.form.alias.$error.max).toBeFalsy();
740+
});
725741
});
726742
});
727743

@@ -886,6 +902,14 @@ describe('input', function() {
886902
expect(inputElm).toBeInvalid();
887903
expect($rootScope.form.alias.$error.min).toBeTruthy();
888904
});
905+
906+
it('should validate if min is empty', function() {
907+
$rootScope.minVal = undefined;
908+
$rootScope.value = new Date(-9999, 0, 1, 0, 0, 0);
909+
$rootScope.$digest();
910+
911+
expect($rootScope.form.alias.$error.min).toBeFalsy();
912+
});
889913
});
890914

891915
describe('max', function() {
@@ -921,6 +945,14 @@ describe('input', function() {
921945
expect(inputElm).toBeInvalid();
922946
expect($rootScope.form.alias.$error.max).toBeTruthy();
923947
});
948+
949+
it('should validate if max is empty', function() {
950+
$rootScope.maxVal = undefined;
951+
$rootScope.value = new Date(9999, 11, 31, 23, 59, 59);
952+
$rootScope.$digest();
953+
954+
expect($rootScope.form.alias.$error.max).toBeFalsy();
955+
});
924956
});
925957
});
926958

@@ -1119,6 +1151,14 @@ describe('input', function() {
11191151
expect(inputElm).toBeInvalid();
11201152
expect($rootScope.form.alias.$error.min).toBeTruthy();
11211153
});
1154+
1155+
it('should validate if min is empty', function() {
1156+
$rootScope.minVal = undefined;
1157+
$rootScope.value = new Date(-9999, 0, 1, 0, 0, 0);
1158+
$rootScope.$digest();
1159+
1160+
expect($rootScope.form.alias.$error.min).toBeFalsy();
1161+
});
11221162
});
11231163

11241164
describe('max', function() {
@@ -1153,6 +1193,14 @@ describe('input', function() {
11531193
expect(inputElm).toBeInvalid();
11541194
expect($rootScope.form.alias.$error.max).toBeTruthy();
11551195
});
1196+
1197+
it('should validate if max is empty', function() {
1198+
$rootScope.maxVal = undefined;
1199+
$rootScope.value = new Date(9999, 11, 31, 23, 59, 59);
1200+
$rootScope.$digest();
1201+
1202+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1203+
});
11561204
});
11571205

11581206

@@ -1428,12 +1476,21 @@ describe('input', function() {
14281476
expect(inputElm).toBeInvalid();
14291477
expect($rootScope.form.alias.$error.min).toBeTruthy();
14301478
});
1479+
1480+
it('should validate if min is empty', function() {
1481+
$rootScope.minVal = undefined;
1482+
$rootScope.value = new Date(-9999, 0, 1, 0, 0, 0);
1483+
$rootScope.$digest();
1484+
1485+
expect($rootScope.form.alias.$error.min).toBeFalsy();
1486+
});
14311487
});
14321488

14331489
describe('max', function() {
14341490
var inputElm;
14351491
beforeEach(function() {
1436-
inputElm = helper.compileInput('<input type="time" ng-model="value" name="alias" max="22:30:00" />');
1492+
$rootScope.maxVal = '22:30:00';
1493+
inputElm = helper.compileInput('<input type="time" ng-model="value" name="alias" max="{{ maxVal }}" />');
14371494
});
14381495

14391496
it('should invalidate', function() {
@@ -1449,11 +1506,19 @@ describe('input', function() {
14491506
expect(+$rootScope.value).toBe(+new Date(1970, 0, 1, 5, 30, 0));
14501507
expect($rootScope.form.alias.$error.max).toBeFalsy();
14511508
});
1509+
1510+
it('should validate if max is empty', function() {
1511+
$rootScope.maxVal = undefined;
1512+
$rootScope.value = new Date(9999, 11, 31, 23, 59, 59);
1513+
$rootScope.$digest();
1514+
1515+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1516+
});
14521517
});
14531518

14541519

14551520
it('should validate even if max value changes on-the-fly', function() {
1456-
$rootScope.max = '4:02:00';
1521+
$rootScope.max = '04:02:00';
14571522
var inputElm = helper.compileInput('<input type="time" ng-model="value" name="alias" max="{{max}}" />');
14581523

14591524
helper.changeInputValueTo('05:34:00');
@@ -1481,7 +1546,7 @@ describe('input', function() {
14811546

14821547

14831548
it('should validate even if ng-max value changes on-the-fly', function() {
1484-
$rootScope.max = '4:02:00';
1549+
$rootScope.max = '04:02:00';
14851550
var inputElm = helper.compileInput('<input type="time" ng-model="value" name="alias" ng-max="max" />');
14861551

14871552
helper.changeInputValueTo('05:34:00');
@@ -1706,6 +1771,16 @@ describe('input', function() {
17061771

17071772
expect($rootScope.form.myControl.$error.min).toBeTruthy();
17081773
});
1774+
1775+
it('should validate if min is empty', function() {
1776+
var inputElm = helper.compileInput(
1777+
'<input type="date" name="alias" ng-model="value" min />');
1778+
1779+
$rootScope.value = new Date(-9999, 0, 1, 0, 0, 0);
1780+
$rootScope.$digest();
1781+
1782+
expect($rootScope.form.alias.$error.min).toBeFalsy();
1783+
});
17091784
});
17101785

17111786
describe('max', function() {
@@ -1735,6 +1810,16 @@ describe('input', function() {
17351810

17361811
expect($rootScope.form.myControl.$error.max).toBeTruthy();
17371812
});
1813+
1814+
it('should validate if max is empty', function() {
1815+
var inputElm = helper.compileInput(
1816+
'<input type="date" name="alias" ng-model="value" max />');
1817+
1818+
$rootScope.value = new Date(9999, 11, 31, 23, 59, 59);
1819+
$rootScope.$digest();
1820+
1821+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1822+
});
17381823
});
17391824

17401825

0 commit comments

Comments
 (0)