After receiving data from an API, I aim to build a reactive form with a parent form and multiple child forms. The data that needs to be displayed in the form has the following structure:
data{
"extraInformation": false
"cars": [
{
"Id": 48,
"Period": {
"Start": null,
"End": null },
"Rentalstats": null,
"Reason": null
}
]
}
How can I achieve this? This is what I have attempted so far:
this.form = this.formBuilder.group({
extraInformation: [this.data.ExtraInformation, [Validators.required]],
cars: this.fb.array([this.addDetailFormGroup()]),
});
addDetailFormGroup() {
this.form = new FormGroup({
Reason: new FormControl(this.data?.cars?.Reason, Validators.required),
Rentalstats: new FormControl(this.data?.cars?.Rentalstats, Validators.required),
Period: new FormGroup([this.initListFormGroup()]),
});
initListFormGroup() {
return new FormGroup({
Start: new FormControl(this.data?.cars?.Period?.Start, Validators.required),
End: new FormControl(this.data?.cars?.Period?.End, Validators.required),
});
}
However, I am encountering an issue where I get a "Cannot read properties of undefined (reading 'Start')" error.
Any assistance on this matter would be greatly appreciated.