Skip to content

Commit ee5a110

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 ee5a110

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-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 (timezone) {
1499+
parsedDate = convertTimezoneToLocal(parsedDate, timezone);
1500+
}
1501+
return parsedDate;
14981502
}
14991503
};
15001504
}

test/ng/directive/inputSpec.js

+68
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,23 @@ 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: \'UTC\', allowInvalid: true}"/>');
1097+
$rootScope.maxVal = '2013-W01';
1098+
$rootScope.value = new Date(Date.UTC(2013, 0, 1, 0, 0, 0));
1099+
$rootScope.$digest();
1100+
1101+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1102+
expect($rootScope.form.alias.$valid).toBeTruthy();
1103+
1104+
$rootScope.value = '';
1105+
helper.changeInputValueTo('2013-W01');
1106+
expect(inputElm).toBeValid();
1107+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1108+
expect($rootScope.form.alias.$valid).toBeTruthy();
1109+
});
10761110
});
10771111
});
10781112

@@ -1342,6 +1376,23 @@ describe('input', function() {
13421376

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

13471398

@@ -1660,6 +1711,23 @@ describe('input', function() {
16601711

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

16651733

0 commit comments

Comments
 (0)