Skip to content

Commit 08604c0

Browse files
committed
fix(input): take timezone into account when validating minimum and maximum date spans
Closes angular#16342 Closes angular#16390
1 parent 07d84dd commit 08604c0

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
@@ -1440,11 +1440,7 @@ function createDateInputType(type, regexp, parseDate, format) {
14401440
// Note: We cannot read ctrl.$modelValue, as there might be a different
14411441
// parser/formatter in the processing chain so that the model
14421442
// contains some different data format!
1443-
var parsedDate = parseDate(value, previousDate);
1444-
if (timezone) {
1445-
parsedDate = convertTimezoneToLocal(parsedDate, timezone);
1446-
}
1447-
return parsedDate;
1443+
return parseDateAndConvertTimeZoneToLocal(value, previousDate);
14481444
}
14491445
ctrl.$$parserName = type;
14501446
return undefined;
@@ -1494,7 +1490,15 @@ function createDateInputType(type, regexp, parseDate, format) {
14941490
}
14951491

14961492
function parseObservedDateValue(val) {
1497-
return isDefined(val) && !isDate(val) ? parseDate(val) || undefined : val;
1493+
return isDefined(val) && !isDate(val) ? parseDateAndConvertTimeZoneToLocal(val) || undefined : val;
1494+
}
1495+
1496+
function parseDateAndConvertTimeZoneToLocal(value, previousDate) {
1497+
var parsedDate = parseDate(value, previousDate);
1498+
if (!isNaN(parsedDate) && timezone) {
1499+
parsedDate = convertTimezoneToLocal(parsedDate, timezone);
1500+
}
1501+
return parsedDate;
14981502
}
14991503
};
15001504
}

test/ng/directive/inputSpec.js

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

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

@@ -1073,6 +1090,25 @@ describe('input', function() {
10731090

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

@@ -1342,6 +1378,23 @@ describe('input', function() {
13421378

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

13471400

@@ -1660,6 +1713,23 @@ describe('input', function() {
16601713

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

16651735

@@ -2005,6 +2075,24 @@ describe('input', function() {
20052075

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

20102098

0 commit comments

Comments
 (0)