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

Commit 71976be

Browse files
committed
fix(datepicker): throw error if model is not a Date. For #5266
1 parent 27d1867 commit 71976be

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/components/datepicker/datePicker.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,15 @@
251251

252252
var self = this;
253253
ngModelCtrl.$render = function() {
254-
self.date = self.ngModelCtrl.$viewValue;
255-
self.inputElement.value = self.dateLocale.formatDate(self.date);
254+
var value = self.ngModelCtrl.$viewValue;
255+
256+
if (value && !(value instanceof Date)) {
257+
throw Error('The ng-model for md-datepicker must be a Date instance. ' +
258+
'Currently the model is a: ' + (typeof value));
259+
}
260+
261+
self.date = value;
262+
self.inputElement.value = self.dateLocale.formatDate(value);
256263
self.resizeInputElement();
257264
self.setErrorFlags();
258265
};

src/components/datepicker/datePicker.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ describe('md-date-picker', function() {
9999
expect(controller.inputElement.placeholder).toBe('Fancy new placeholder');
100100
});
101101

102+
it('should throw an error when the model is not a date', function() {
103+
expect(function() {
104+
pageScope.myDate = '2015-01-01';
105+
pageScope.$apply();
106+
}).toThrowError('The ng-model for md-datepicker must be a Date instance. ' +
107+
'Currently the model is a: string');
108+
});
109+
110+
it('should support null and undefined values', function() {
111+
expect(function() {
112+
pageScope.myDate = null;
113+
pageScope.$apply();
114+
115+
pageScope.myDate = undefined;
116+
pageScope.$apply();
117+
118+
}).not.toThrow();
119+
});
120+
102121
it('should throw an error when inside of md-input-container', function() {
103122
var template =
104123
'<md-input-container>' +

0 commit comments

Comments
 (0)