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

Commit 0a06f99

Browse files
committed
fix(datepicker): ISO 8601 dates decorated as invalid in forms
- when in `updateErrorState()`, need to make sure that ISO 8601 strings are parsed to dates Fixes #12075
1 parent 901982b commit 0a06f99

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/components/datepicker/js/datepickerDirective.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,11 @@
652652
if (opt_date) {
653653
date = new Date(opt_date.valueOf());
654654
} else {
655-
date = angular.copy(this.ngModelCtrl.$modelValue);
655+
if (angular.isString(this.ngModelCtrl.$modelValue)) {
656+
date = new Date(this.ngModelCtrl.$modelValue);
657+
} else {
658+
date = angular.copy(this.ngModelCtrl.$modelValue);
659+
}
656660
}
657661

658662
// Clear any existing errors to get rid of anything that's no longer relevant.

src/components/datepicker/js/datepickerDirective.spec.js

+46-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ var DATEPICKER_TEMPLATE =
2222
'ng-disabled="isDisabled">' +
2323
'</md-datepicker>';
2424

25+
var DATEPICKER_FORM_TEMPLATE =
26+
'<form name="birthdayForm">' +
27+
' <md-datepicker name="birthday" ' +
28+
' md-max-date="maxDate" ' +
29+
' md-min-date="minDate" ' +
30+
' md-date-filter="dateFilter" ' +
31+
' md-month-filter="monthFilter" ' +
32+
' ng-model="myDate" ' +
33+
' ng-change="dateChangedHandler()" ' +
34+
' ng-focus="focusHandler()" ' +
35+
' ng-blur="blurHandler()" ' +
36+
' ng-required="isRequired" ' +
37+
' ng-disabled="isDisabled">' +
38+
' </md-datepicker>' +
39+
'</form>';
40+
2541
/**
2642
* Compile and link the given template and store values for element, scope, and controller.
2743
* @param {string} template
@@ -135,6 +151,35 @@ describe('md-datepicker', function() {
135151
}).not.toThrow();
136152
});
137153

154+
it('should support null, undefined, and values that can be parsed into a date in a form',
155+
function() {
156+
var formElement = createDatepickerInstance(DATEPICKER_FORM_TEMPLATE);
157+
var datepickerInputContainer =
158+
formElement[0].querySelector('md-datepicker .md-datepicker-input-container');
159+
160+
pageScope.myDate = null;
161+
pageScope.$apply();
162+
$timeout.flush();
163+
expect(datepickerInputContainer.classList.contains('md-datepicker-invalid')).toBeFalsy();
164+
165+
pageScope.myDate = undefined;
166+
pageScope.$apply();
167+
$timeout.flush();
168+
expect(datepickerInputContainer.classList.contains('md-datepicker-invalid')).toBeFalsy();
169+
170+
pageScope.myDate = '2016-09-08';
171+
pageScope.$apply();
172+
$timeout.flush();
173+
expect(pageScope.myDate).toEqual('2016-09-08');
174+
expect(datepickerInputContainer.classList.contains('md-datepicker-invalid')).toBeFalsy();
175+
176+
pageScope.myDate = '2021-01-20T07:00:00Z';
177+
pageScope.$apply();
178+
$timeout.flush();
179+
expect(pageScope.myDate).toEqual('2021-01-20T07:00:00Z');
180+
expect(datepickerInputContainer.classList.contains('md-datepicker-invalid')).toBeFalsy();
181+
});
182+
138183
it('should set the element type as "date"', function() {
139184
expect(ngElement.attr('type')).toBe('date');
140185
});
@@ -448,7 +493,7 @@ describe('md-datepicker', function() {
448493
expect(controller.ngModelCtrl.$touched).toBe(true);
449494
});
450495

451-
it('should not update the input string is not "complete"', function() {
496+
it('should not update the input string if not "complete"', function() {
452497
var date = new Date(2015, DEC, 1);
453498
pageScope.myDate = date;
454499

0 commit comments

Comments
 (0)