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

Commit 2f0ac69

Browse files
m-amrNarretz
authored andcommitted
fix(input): take timezone into account when validating minimum and maximum in date types
Closes #16342 Closes #16390
1 parent da3d96c commit 2f0ac69

File tree

2 files changed

+98
-6
lines changed

2 files changed

+98
-6
lines changed

src/ng/directive/input.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1474,11 +1474,7 @@ function createDateInputType(type, regexp, parseDate, format) {
14741474
// Note: We cannot read ctrl.$modelValue, as there might be a different
14751475
// parser/formatter in the processing chain so that the model
14761476
// contains some different data format!
1477-
var parsedDate = parseDate(value, previousDate);
1478-
if (timezone) {
1479-
parsedDate = convertTimezoneToLocal(parsedDate, timezone);
1480-
}
1481-
return parsedDate;
1477+
return parseDateAndConvertTimeZoneToLocal(value, previousDate);
14821478
}
14831479
return undefined;
14841480
});
@@ -1527,7 +1523,15 @@ function createDateInputType(type, regexp, parseDate, format) {
15271523
}
15281524

15291525
function parseObservedDateValue(val) {
1530-
return isDefined(val) && !isDate(val) ? parseDate(val) || undefined : val;
1526+
return isDefined(val) && !isDate(val) ? parseDateAndConvertTimeZoneToLocal(val) || undefined : val;
1527+
}
1528+
1529+
function parseDateAndConvertTimeZoneToLocal(value, previousDate) {
1530+
var parsedDate = parseDate(value, previousDate);
1531+
if (!isNaN(parsedDate) && timezone) {
1532+
parsedDate = convertTimezoneToLocal(parsedDate, timezone);
1533+
}
1534+
return parsedDate;
15311535
}
15321536
};
15331537
}

test/ng/directive/inputSpec.js

+88
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,23 @@ describe('input', function() {
836836

837837
expect($rootScope.form.alias.$error.max).toBeFalsy();
838838
});
839+
840+
it('should validate when timezone is provided.', function() {
841+
inputElm = helper.compileInput('<input type="month" ng-model="value" name="alias" ' +
842+
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');
843+
$rootScope.maxVal = '2013-01';
844+
$rootScope.value = new Date(Date.UTC(2013, 0, 1, 0, 0, 0));
845+
$rootScope.$digest();
846+
847+
expect($rootScope.form.alias.$error.max).toBeFalsy();
848+
expect($rootScope.form.alias.$valid).toBeTruthy();
849+
850+
$rootScope.value = '';
851+
helper.changeInputValueTo('2013-01');
852+
expect(inputElm).toBeValid();
853+
expect($rootScope.form.alias.$error.max).toBeFalsy();
854+
expect($rootScope.form.alias.$valid).toBeTruthy();
855+
});
839856
});
840857
});
841858

@@ -1069,6 +1086,25 @@ describe('input', function() {
10691086

10701087
expect($rootScope.form.alias.$error.max).toBeFalsy();
10711088
});
1089+
1090+
it('should validate when timezone is provided.', function() {
1091+
inputElm = helper.compileInput('<input type="week" ng-model="value" name="alias" ' +
1092+
'max="{{ maxVal }}" ng-model-options="{timezone: \'-2400\', allowInvalid: true}"/>');
1093+
// The calendar week comparison date is January 17. Setting the timezone to -2400
1094+
// makes the January 18 date value valid.
1095+
$rootScope.maxVal = '2013-W03';
1096+
$rootScope.value = new Date(Date.UTC(2013, 0, 18));
1097+
$rootScope.$digest();
1098+
1099+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1100+
expect($rootScope.form.alias.$valid).toBeTruthy();
1101+
1102+
$rootScope.value = '';
1103+
helper.changeInputValueTo('2013-W03');
1104+
expect(inputElm).toBeValid();
1105+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1106+
expect($rootScope.form.alias.$valid).toBeTruthy();
1107+
});
10721108
});
10731109
});
10741110

@@ -1338,6 +1374,23 @@ describe('input', function() {
13381374

13391375
expect($rootScope.form.alias.$error.max).toBeFalsy();
13401376
});
1377+
1378+
it('should validate when timezone is provided.', function() {
1379+
inputElm = helper.compileInput('<input type="datetime-local" ng-model="value" name="alias" ' +
1380+
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');
1381+
$rootScope.maxVal = '2013-01-01T00:00:00';
1382+
$rootScope.value = new Date(Date.UTC(2013, 0, 1, 0, 0, 0));
1383+
$rootScope.$digest();
1384+
1385+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1386+
expect($rootScope.form.alias.$valid).toBeTruthy();
1387+
1388+
$rootScope.value = '';
1389+
helper.changeInputValueTo('2013-01-01T00:00:00');
1390+
expect(inputElm).toBeValid();
1391+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1392+
expect($rootScope.form.alias.$valid).toBeTruthy();
1393+
});
13411394
});
13421395

13431396

@@ -1656,6 +1709,23 @@ describe('input', function() {
16561709

16571710
expect($rootScope.form.alias.$error.max).toBeFalsy();
16581711
});
1712+
1713+
it('should validate when timezone is provided.', function() {
1714+
inputElm = helper.compileInput('<input type="time" ng-model="value" name="alias" ' +
1715+
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');
1716+
$rootScope.maxVal = '22:30:00';
1717+
$rootScope.value = new Date(Date.UTC(1970, 0, 1, 22, 30, 0));
1718+
$rootScope.$digest();
1719+
1720+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1721+
expect($rootScope.form.alias.$valid).toBeTruthy();
1722+
1723+
$rootScope.value = '';
1724+
helper.changeInputValueTo('22:30:00');
1725+
expect(inputElm).toBeValid();
1726+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1727+
expect($rootScope.form.alias.$valid).toBeTruthy();
1728+
});
16591729
});
16601730

16611731

@@ -2001,6 +2071,24 @@ describe('input', function() {
20012071

20022072
expect($rootScope.form.alias.$error.max).toBeFalsy();
20032073
});
2074+
2075+
it('should validate when timezone is provided.', function() {
2076+
var inputElm = helper.compileInput('<input type="date" ng-model="value" name="alias" ' +
2077+
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');
2078+
2079+
$rootScope.maxVal = '2013-12-01';
2080+
$rootScope.value = new Date(Date.UTC(2013, 11, 1, 0, 0, 0));
2081+
$rootScope.$digest();
2082+
2083+
expect($rootScope.form.alias.$error.max).toBeFalsy();
2084+
expect($rootScope.form.alias.$valid).toBeTruthy();
2085+
2086+
$rootScope.value = '';
2087+
helper.changeInputValueTo('2013-12-01');
2088+
expect(inputElm).toBeValid();
2089+
expect($rootScope.form.alias.$error.max).toBeFalsy();
2090+
expect($rootScope.form.alias.$valid).toBeTruthy();
2091+
});
20042092
});
20052093

20062094

0 commit comments

Comments
 (0)