@@ -34,6 +34,7 @@ import {
3434import { DateAdapter , MD_DATE_FORMATS , MdDateFormats } from '@angular/material/core' ;
3535import { MdFormField } from '@angular/material/form-field' ;
3636import { Subscription } from 'rxjs/Subscription' ;
37+ import { coerceDateProperty } from './coerce-date-property' ;
3738import { MdDatepicker } from './datepicker' ;
3839import { createMissingDateImplError } from './datepicker-errors' ;
3940
@@ -74,8 +75,8 @@ export class MdDatepickerInputEvent<D> {
7475 host : {
7576 '[attr.aria-haspopup]' : 'true' ,
7677 '[attr.aria-owns]' : '(_datepicker?.opened && _datepicker.id) || null' ,
77- '[attr.min]' : 'min ? _dateAdapter.toISODateString (min) : null' ,
78- '[attr.max]' : 'max ? _dateAdapter.toISODateString (max) : null' ,
78+ '[attr.min]' : 'min ? _dateAdapter.toIso8601 (min) : null' ,
79+ '[attr.max]' : 'max ? _dateAdapter.toIso8601 (max) : null' ,
7980 '[disabled]' : 'disabled' ,
8081 '(input)' : '_onInput($event.target.value)' ,
8182 '(change)' : '_onChange()' ,
@@ -122,10 +123,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
122123 return this . _value ;
123124 }
124125 set value ( value : D | null ) {
125- value = this . _coerceDateProperty ( value ) ;
126- if ( value != null && ! this . _dateAdapter . isDateInstance ( value ) ) {
127- throw Error ( 'Datepicker: value not recognized as a date object by DateAdapter.' ) ;
128- }
126+ value = coerceDateProperty ( this . _dateAdapter , value ) ;
129127 this . _lastValueValid = ! value || this . _dateAdapter . isValid ( value ) ;
130128 value = this . _getValidDateOrNull ( value ) ;
131129
@@ -143,7 +141,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
143141 @Input ( )
144142 get min ( ) : D | null { return this . _min ; }
145143 set min ( value : D | null ) {
146- this . _min = this . _coerceDateProperty ( value ) ;
144+ this . _min = coerceDateProperty ( this . _dateAdapter , value ) ;
147145 this . _validatorOnChange ( ) ;
148146 }
149147 private _min : D | null ;
@@ -152,7 +150,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
152150 @Input ( )
153151 get max ( ) : D | null { return this . _max ; }
154152 set max ( value : D | null ) {
155- this . _max = this . _coerceDateProperty ( value ) ;
153+ this . _max = coerceDateProperty ( this . _dateAdapter , value ) ;
156154 this . _validatorOnChange ( ) ;
157155 }
158156 private _max : D | null ;
@@ -200,23 +198,23 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
200198
201199 /** The form control validator for the min date. */
202200 private _minValidator : ValidatorFn = ( control : AbstractControl ) : ValidationErrors | null => {
203- const controlValue = this . _coerceDateProperty ( control . value ) ;
201+ const controlValue = coerceDateProperty ( this . _dateAdapter , control . value ) ;
204202 return ( ! this . min || ! controlValue ||
205203 this . _dateAdapter . compareDate ( this . min , controlValue ) <= 0 ) ?
206- null : { 'mdDatepickerMin' : { 'min' : this . min , 'actual' : control . value } } ;
204+ null : { 'mdDatepickerMin' : { 'min' : this . min , 'actual' : controlValue } } ;
207205 }
208206
209207 /** The form control validator for the max date. */
210208 private _maxValidator : ValidatorFn = ( control : AbstractControl ) : ValidationErrors | null => {
211- const controlValue = this . _coerceDateProperty ( control . value ) ;
209+ const controlValue = coerceDateProperty ( this . _dateAdapter , control . value ) ;
212210 return ( ! this . max || ! controlValue ||
213211 this . _dateAdapter . compareDate ( this . max , controlValue ) >= 0 ) ?
214- null : { 'mdDatepickerMax' : { 'max' : this . max , 'actual' : control . value } } ;
212+ null : { 'mdDatepickerMax' : { 'max' : this . max , 'actual' : controlValue } } ;
215213 }
216214
217215 /** The form control validator for the date filter. */
218216 private _filterValidator : ValidatorFn = ( control : AbstractControl ) : ValidationErrors | null => {
219- const controlValue = this . _coerceDateProperty ( control . value ) ;
217+ const controlValue = coerceDateProperty ( this . _dateAdapter , control . value ) ;
220218 return ! this . _dateFilter || ! controlValue || this . _dateFilter ( controlValue ) ?
221219 null : { 'mdDatepickerFilter' : true } ;
222220 }
@@ -332,16 +330,4 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
332330 private _getValidDateOrNull ( obj : any ) : D | null {
333331 return ( this . _dateAdapter . isDateInstance ( obj ) && this . _dateAdapter . isValid ( obj ) ) ? obj : null ;
334332 }
335-
336- /**
337- * Attempts to coerce a property to a date by parsing it as a ISO 8601 string. If not a valid
338- * ISO 8601 string, returns the original vlaue.
339- */
340- private _coerceDateProperty ( value : any ) : any {
341- if ( typeof value === 'string' ) {
342- const d = this . _dateAdapter . fromISODateString ( value ) ;
343- return d || value ;
344- }
345- return value ;
346- }
347333}
0 commit comments