Below is the code for my component:
this.participantForm = this.fb.group({
occupation: [null],
consent : new FormGroup({
consentBy: new FormControl(''),
consentDate: new FormControl(new Date())
})
})
This is the HTML section:
<form [formGroup]="participantForm">
<div formGroupName="consent">
<label>
Name:
<input type="text" formControlName="consentBy">
</label>
<label>
Date:
<input type="text" formControlName="consentDate">
</label>
</div>
</form>
After submission, the date value needs to be formatted. Here is the relevant code snippet:
get pfc() {
return this.participantForm.controls;
}
this.participantForm.patchValue({
consent: {
consentDate : moment(this.pfc.consent.consentDate.value, "DD-MMM-YYY HH:mm").format(
"DD-MMM-YYYY HH:mm")
}
});
An error occurs:
ERROR TypeError: Cannot read property 'consentDate' of undefined.
The issue lies in consent being undefined. How can I rectify this and update the form value?