Angular material datepicker ignores date when manually typed. If I choose the date on picker modal the validator receives the right value, but when I insert it manually the validator receives null.
Stackblitz problem example : https://stackblitz.com/edit/angular-pglzxk?file=src/app/datepicker-overview-example.html
Html
<mat-form-field >
<mat-label>Choose a date</mat-label>
<input matInput [matDatepicker]="picker" formControlName="date">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
Reactive Form
this.form = new FormGroup({
date: new FormControl(new Date(new Date()), [DateValidator()]),
});
Validator
export function DateValidator(format = "dd/MM/yyyy"): any {
return (control: FormControl): { [key: string]: any } => {
const val = moment(control.value, format, true);
console.log(control.value);
if (!val.isValid()) {
return {invalidDate: true};
}
return {};
};
}