From data that I recive from API I want to create a reactive form. I have a formparent and multiple childforms. The data that I want to display in form looks like this:
data{
"extraInformation": false
"cars": [
{
"Id": 48,
"Period": {
"Start": null,
"End": null },
"Rentalstats": null,
"Reason": null
}
]
}
How do I achieve this? So far this is what I have done:
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),
});
}
But it dosent work because I get "Cannot read properties of undefined (reading 'Start')..
I would really appreciate it if someone could help