I am trying to bind my date property to mat-datepicker's input as part of a reactive form group. All of my methods didn't work, as my submit button is set to disabled unless the form is valid:
<mat-form-field fxFlex> <input matInput [matDatepicker]="datepicker1" placeholder="Start Date" formControlName="startingDate" required> <mat-datepicker-toggle matSuffix [for]="datepicker1"></mat-datepicker-toggle> <mat-datepicker touchUi="true" #datepicker1></mat-datepicker> </mat-form-field> Component.ts:
start: Date.now() startDate: "1/1/2018" this.ShiftForm = new FormGroup({ startingDate: new FormControl(this.startDate), startTime: new FormControl(), endingDate: new FormControl(this.endDate), endTime: new FormControl(), }); Methods which didn't work:
- Adding
[(ngModel)]="startDate"to input field - Adding
[ngModel]="startDate"to input field - Preloading the formControl with the value:
startingDate: new FormControl(this.startDate),
Methods which partially but not satisfyingly worked:
- Adding
[value]="startDate"to input field: date showed but not read by the Reactive Form, meaning my submit button remains disabled as the form is not valid. To make it valid, I have to set the date manually (typing or using the datepicker) - Adding
[ngModel]="startDate"while removing[matDatepicker]="datepicker1"from the input field: date showed but removing the access to the datepicker.
All I need is the [ngModel] to work as it should so the Reactive Form reads it.
Help much much appreciated!
EDIT This is my formGroup:
this.ShiftForm = new FormGroup({ startingDate: new FormControl(this.startDate), startTime: new FormControl(), endingDate: new FormControl(this.endDate), endTime: new FormControl(), }); This is the HTML:
<form [formGroup]="ShiftForm" fxLayout="column" fxLayoutAlign="center stretch"> <mat-form-field fxFlex> <input matInput [matDatepicker]="datepicker1" placeholder="Start Date" [formControlName]="startingDate" required> <mat-datepicker-toggle matSuffix [for]="datepicker1"></mat-datepicker-toggle> <mat-datepicker touchUi="true" #datepicker1></mat-datepicker> </mat-form-field> .. .. .. </form> This is the Error I get now:
Error: Cannot find control with unspecified name attribute