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

fix(input): Take timezone into account when validating minimum and ma… #16390

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1440,11 +1440,7 @@ function createDateInputType(type, regexp, parseDate, format) {
// Note: We cannot read ctrl.$modelValue, as there might be a different
// parser/formatter in the processing chain so that the model
// contains some different data format!
var parsedDate = parseDate(value, previousDate);
if (timezone) {
parsedDate = convertTimezoneToLocal(parsedDate, timezone);
}
return parsedDate;
return parseDateAndConvertTimeZoneToLocal(value, previousDate);
}
ctrl.$$parserName = type;
return undefined;
Expand Down Expand Up @@ -1494,7 +1490,15 @@ function createDateInputType(type, regexp, parseDate, format) {
}

function parseObservedDateValue(val) {
return isDefined(val) && !isDate(val) ? parseDate(val) || undefined : val;
return isDefined(val) && !isDate(val) ? parseDateAndConvertTimeZoneToLocal(val) || undefined : val;
}

function parseDateAndConvertTimeZoneToLocal(value, previousDate) {
var parsedDate = parseDate(value, previousDate);
if (!isNaN(parsedDate) && timezone) {
parsedDate = convertTimezoneToLocal(parsedDate, timezone);
}
return parsedDate;
}
};
}
Expand Down
88 changes: 88 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,23 @@ describe('input', function() {

expect($rootScope.form.alias.$error.max).toBeFalsy();
});

it('should validate when timezone is provided.', function() {
inputElm = helper.compileInput('<input type="month" ng-model="value" name="alias" ' +
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');
$rootScope.maxVal = '2013-01';
$rootScope.value = new Date(Date.UTC(2013, 0, 1, 0, 0, 0));
$rootScope.$digest();

expect($rootScope.form.alias.$error.max).toBeFalsy();
expect($rootScope.form.alias.$valid).toBeTruthy();

$rootScope.value = '';
helper.changeInputValueTo('2013-01');
expect(inputElm).toBeValid();
expect($rootScope.form.alias.$error.max).toBeFalsy();
expect($rootScope.form.alias.$valid).toBeTruthy();
});
});
});

Expand Down Expand Up @@ -1073,6 +1090,25 @@ describe('input', function() {

expect($rootScope.form.alias.$error.max).toBeFalsy();
});

it('should validate when timezone is provided.', function() {
inputElm = helper.compileInput('<input type="week" ng-model="value" name="alias" ' +
'max="{{ maxVal }}" ng-model-options="{timezone: \'-2400\', allowInvalid: true}"/>');
// The calendar week comparison date is January 17. Setting the timezone to -2400
// makes the January 18 date value valid.
$rootScope.maxVal = '2013-W03';
$rootScope.value = new Date(Date.UTC(2013, 0, 18));
$rootScope.$digest();

expect($rootScope.form.alias.$error.max).toBeFalsy();
expect($rootScope.form.alias.$valid).toBeTruthy();

$rootScope.value = '';
helper.changeInputValueTo('2013-W03');
expect(inputElm).toBeValid();
expect($rootScope.form.alias.$error.max).toBeFalsy();
expect($rootScope.form.alias.$valid).toBeTruthy();
});
});
});

Expand Down Expand Up @@ -1342,6 +1378,23 @@ describe('input', function() {

expect($rootScope.form.alias.$error.max).toBeFalsy();
});

it('should validate when timezone is provided.', function() {
inputElm = helper.compileInput('<input type="datetime-local" ng-model="value" name="alias" ' +
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');
$rootScope.maxVal = '2013-01-01T00:00:00';
$rootScope.value = new Date(Date.UTC(2013, 0, 1, 0, 0, 0));
$rootScope.$digest();

expect($rootScope.form.alias.$error.max).toBeFalsy();
expect($rootScope.form.alias.$valid).toBeTruthy();

$rootScope.value = '';
helper.changeInputValueTo('2013-01-01T00:00:00');
expect(inputElm).toBeValid();
expect($rootScope.form.alias.$error.max).toBeFalsy();
expect($rootScope.form.alias.$valid).toBeTruthy();
});
});


Expand Down Expand Up @@ -1660,6 +1713,23 @@ describe('input', function() {

expect($rootScope.form.alias.$error.max).toBeFalsy();
});

it('should validate when timezone is provided.', function() {
inputElm = helper.compileInput('<input type="time" ng-model="value" name="alias" ' +
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');
$rootScope.maxVal = '22:30:00';
$rootScope.value = new Date(Date.UTC(1970, 0, 1, 22, 30, 0));
$rootScope.$digest();

expect($rootScope.form.alias.$error.max).toBeFalsy();
expect($rootScope.form.alias.$valid).toBeTruthy();

$rootScope.value = '';
helper.changeInputValueTo('22:30:00');
expect(inputElm).toBeValid();
expect($rootScope.form.alias.$error.max).toBeFalsy();
expect($rootScope.form.alias.$valid).toBeTruthy();
});
});


Expand Down Expand Up @@ -2005,6 +2075,24 @@ describe('input', function() {

expect($rootScope.form.alias.$error.max).toBeFalsy();
});

it('should validate when timezone is provided.', function() {
var inputElm = helper.compileInput('<input type="date" ng-model="value" name="alias" ' +
'max="{{ maxVal }}" ng-model-options="{timezone: \'UTC\', allowInvalid: true}"/>');

$rootScope.maxVal = '2013-12-01';
$rootScope.value = new Date(Date.UTC(2013, 11, 1, 0, 0, 0));
$rootScope.$digest();

expect($rootScope.form.alias.$error.max).toBeFalsy();
expect($rootScope.form.alias.$valid).toBeTruthy();

$rootScope.value = '';
helper.changeInputValueTo('2013-12-01');
expect(inputElm).toBeValid();
expect($rootScope.form.alias.$error.max).toBeFalsy();
expect($rootScope.form.alias.$valid).toBeTruthy();
});
});


Expand Down