When creating a POST request to an API, I encountered an issue with the mat-datepicker
field as it throws an error when inside the ngOnInit()
call (since nothing is selected yet). Other fields like name
, email
, etc. work fine, but extracting a value from the datepicker field has been challenging. How can I include this value when submitting the form?
This was my attempt:
ngOnInit() {
this.myForm = new FormGroup({
date_of_birth: new FormControl(new Date().toISOString(), [
Validators.required,
this.backendValidation.bind(this, 'date_of_birth')
])
})
}
<mat-form-field>
<mat-label>Date</mat-label>
<input matInput [matDatepicker]="date_of_birth" formControlName="date_of_birth" />
<mat-datepicker-toggle matSuffix [for]="date_of_birth"></mat-datepicker-toggle>
<mat-datepicker #date_of_birth></mat-datepicker>
</mat-form-field>