Please assist, I have nested form array, se below :
this.form = this.formBuilder.group({
projectTitle: ['', [Validators.required, Validators.maxLength(300)]],
projectDescription: ['', [Validators.required, Validators.maxLength(300)]],
funding: this.formBuilder.array([this._buildFundingItems()]),
});
the this._buildFundingItems() is as follows
private _buildFundingItems(): FormGroup {
return this.formBuilder.group({
items: ['', [Validators.required, Validators.pattern(this.regexValidation.shortWordRegex)]],,
amount: ['', [Validators.required, Validators.pattern(this.regexValidation.amountTypeRegex)]],
});
}
// after reloading the page, i get data from api and I tried setting the value as follows
this.form.setControl('funding', this.formBuilder.array(data.funding || []));
doing the above or this.form.setControl('funding', this.formBuilder.array(data.funding || [])); i getting an error : Cannot find control with path: 'funding -> 0 -> amount' and Cannot find control with path: 'funding -> 0 -> items'.
Before i did the following below, i was not receiving any errors but when updating the form or (on valuechanges), the formarray was not updating.
let length: number = data[0].funding.length;
while (length-- > 0) {
fundingSupport.push(this._buildFundingItems());
}
this.form.controls['funding'] = this.formBuilder.array([]);
this.form.controls['funding'].patchValue(data.funding)
I saw the following link Angular 4 patchValue based on index in FormArray that's why i tried the first approach.