I've been using FormArray in angular reactive form, and everything works just fine until I try to do patchValue() operation.
Here, I try to edit projectForm. In the form you can add multiple developers from <select> <option> </option> </select>. Value in the option field are from the database and the user can add any numbers of developers as he/she wants.
Simplified Code version of the problem is as below:
project.component.ts
projectForm: FormGroup
constructor(private fb: FormBuilder, private projectService: ProjectService, private developerService: DeveloperService){ }
ngOnInit() {
this.developerService.getDeveloper().subscribe((data) => {
this.developers = data;
}, (error) => {
console.log(error)
}
});
this.projectForm = this.fb.group({
project_name: [''],
devs: this.fb.array([this.buildDevs()]),
})
this.editProject();
}
buildDevs(): FormGroup {
return this.fb.group({
developer: ['', Validators.required]
});
}
displayProject(id: number) {
this.projectService.getProjectDetail(id).subscribe(
(project) => this.editProject(project),
(error) => console.log(error)
)
}
editProject(project){
this.projectForm.patchValue({
project_name: project.project_name,
});
this.projectForm.setControl('devs', this.fb.array(project.developer || []));
}
ProjectService is responding with correct project response. Nothing wrong in there.
Add and update post need to be done in same form, and adding the form is working fine.
`project.component.html'
<form [formGroup]="projectForm">
<label>Name: </label>
<input type="text" formControlName="project_name">
<label> Developers: </label>
<div formArrayName="devs">
<div *ngFor="let d of projectForm.controls.devs.controls; let i = index" [formGroupName]="i">
<select formControlName="developer" *ngIf="developers">
<option *ngFor="let dev of developers" value="{{ dev.id }}">{{ dev.user.username }}
</option>
</select>
</div>
</div>
</form>
Following error in the console is present in the response whenever I visit the project edit route

The option field number are exact with the number of developers associated with the project want to be edited but the username of the developer is not seen in the option.
ERROR Error: Cannot find control with path: 'devs -> 0 -> developer'
ERROR Error: Cannot find control with path: 'devs -> 1 -> developer'
so on ... as the number of developer ...
Any help will be appreciated as I am new to angular and tried all possible options that I can do. Thank you.