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

Commit b7bb797

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

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
@@ -1477,11 +1477,7 @@ function createDateInputType(type, regexp, parseDate, format) {
14771477
// Note: We cannot read ctrl.$modelValue, as there might be a different
14781478
// parser/formatter in the processing chain so that the model
14791479
// contains some different data format!
1480-
var parsedDate = parseDate(value, previousDate);
1481-
if (timezone) {
1482-
parsedDate = convertTimezoneToLocal(parsedDate, timezone);
1483-
}
1484-
return parsedDate;
1480+
return parseDateAndConvertTimeZoneToLocal(value, previousDate);
14851481
}
14861482
ctrl.$$parserName = type;
14871483
return undefined;
@@ -1531,7 +1527,15 @@ function createDateInputType(type, regexp, parseDate, format) {
15311527
}
15321528

15331529
function parseObservedDateValue(val) {
1534-
return isDefined(val) && !isDate(val) ? parseDate(val) || undefined : val;
1530+
return isDefined(val) && !isDate(val) ? parseDateAndConvertTimeZoneToLocal(val) || undefined : val;
1531+
}
1532+
1533+
function parseDateAndConvertTimeZoneToLocal(value, previousDate) {
1534+
var parsedDate = parseDate(value, previousDate);
1535+
if (!isNaN(parsedDate) && timezone) {
1536+
parsedDate = convertTimezoneToLocal(parsedDate, timezone);
1537+
}
1538+
return parsedDate;
15351539
}
15361540
};
15371541
}

test/ng/directive/inputSpec.js

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

867867
expect($rootScope.form.alias.$error.max).toBeFalsy();
868868
});
869+
870+
it('should validate when timezone is provided.', function() {
871+
inputElm = helper.compileInput('<input type="month" ng-model="value" name="alias" ' +
872+
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');
873+
$rootScope.maxVal = '2013-01';
874+
$rootScope.value = new Date(Date.UTC(2013, 0, 1, 0, 0, 0));
875+
$rootScope.$digest();
876+
877+
expect($rootScope.form.alias.$error.max).toBeFalsy();
878+
expect($rootScope.form.alias.$valid).toBeTruthy();
879+
880+
$rootScope.value = '';
881+
helper.changeInputValueTo('2013-01');
882+
expect(inputElm).toBeValid();
883+
expect($rootScope.form.alias.$error.max).toBeFalsy();
884+
expect($rootScope.form.alias.$valid).toBeTruthy();
885+
});
869886
});
870887
});
871888

@@ -1099,6 +1116,25 @@ describe('input', function() {
10991116

11001117
expect($rootScope.form.alias.$error.max).toBeFalsy();
11011118
});
1119+
1120+
it('should validate when timezone is provided.', function() {
1121+
inputElm = helper.compileInput('<input type="week" ng-model="value" name="alias" ' +
1122+
'max="{{ maxVal }}" ng-model-options="{timezone: \'-2400\', allowInvalid: true}"/>');
1123+
// The calendar week comparison date is January 17. Setting the timezone to -2400
1124+
// makes the January 18 date value valid.
1125+
$rootScope.maxVal = '2013-W03';
1126+
$rootScope.value = new Date(Date.UTC(2013, 0, 18));
1127+
$rootScope.$digest();
1128+
1129+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1130+
expect($rootScope.form.alias.$valid).toBeTruthy();
1131+
1132+
$rootScope.value = '';
1133+
helper.changeInputValueTo('2013-W03');
1134+
expect(inputElm).toBeValid();
1135+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1136+
expect($rootScope.form.alias.$valid).toBeTruthy();
1137+
});
11021138
});
11031139
});
11041140

@@ -1368,6 +1404,23 @@ describe('input', function() {
13681404

13691405
expect($rootScope.form.alias.$error.max).toBeFalsy();
13701406
});
1407+
1408+
it('should validate when timezone is provided.', function() {
1409+
inputElm = helper.compileInput('<input type="datetime-local" ng-model="value" name="alias" ' +
1410+
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');
1411+
$rootScope.maxVal = '2013-01-01T00:00:00';
1412+
$rootScope.value = new Date(Date.UTC(2013, 0, 1, 0, 0, 0));
1413+
$rootScope.$digest();
1414+
1415+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1416+
expect($rootScope.form.alias.$valid).toBeTruthy();
1417+
1418+
$rootScope.value = '';
1419+
helper.changeInputValueTo('2013-01-01T00:00:00');
1420+
expect(inputElm).toBeValid();
1421+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1422+
expect($rootScope.form.alias.$valid).toBeTruthy();
1423+
});
13711424
});
13721425

13731426

@@ -1686,6 +1739,23 @@ describe('input', function() {
16861739

16871740
expect($rootScope.form.alias.$error.max).toBeFalsy();
16881741
});
1742+
1743+
it('should validate when timezone is provided.', function() {
1744+
inputElm = helper.compileInput('<input type="time" ng-model="value" name="alias" ' +
1745+
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');
1746+
$rootScope.maxVal = '22:30:00';
1747+
$rootScope.value = new Date(Date.UTC(1970, 0, 1, 22, 30, 0));
1748+
$rootScope.$digest();
1749+
1750+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1751+
expect($rootScope.form.alias.$valid).toBeTruthy();
1752+
1753+
$rootScope.value = '';
1754+
helper.changeInputValueTo('22:30:00');
1755+
expect(inputElm).toBeValid();
1756+
expect($rootScope.form.alias.$error.max).toBeFalsy();
1757+
expect($rootScope.form.alias.$valid).toBeTruthy();
1758+
});
16891759
});
16901760

16911761

@@ -2031,6 +2101,24 @@ describe('input', function() {
20312101

20322102
expect($rootScope.form.alias.$error.max).toBeFalsy();
20332103
});
2104+
2105+
it('should validate when timezone is provided.', function() {
2106+
var inputElm = helper.compileInput('<input type="date" ng-model="value" name="alias" ' +
2107+
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');
2108+
2109+
$rootScope.maxVal = '2013-12-01';
2110+
$rootScope.value = new Date(Date.UTC(2013, 11, 1, 0, 0, 0));
2111+
$rootScope.$digest();
2112+
2113+
expect($rootScope.form.alias.$error.max).toBeFalsy();
2114+
expect($rootScope.form.alias.$valid).toBeTruthy();
2115+
2116+
$rootScope.value = '';
2117+
helper.changeInputValueTo('2013-12-01');
2118+
expect(inputElm).toBeValid();
2119+
expect($rootScope.form.alias.$error.max).toBeFalsy();
2120+
expect($rootScope.form.alias.$valid).toBeTruthy();
2121+
});
20342122
});
20352123

20362124

0 commit comments

Comments
 (0)