I need to implement reactive form validation for a form that has dynamic inputs created through looping data:
This is what my form builder setup would be like :
constructor(private formBuilder: FormBuilder) {
this.userForm = this.formBuilder.group({
'inputOne': ['', [Validators.required]],
'inputTwo': ['', [Validators.required]],
...
'inputN': ['', [Validators.required]]
});
}
And this is how my template structure would look like :
<form [formGroup]="userForm">
<div class="form-group" *ngFor="let item of items; let i=index;">
<label for="lastName">{{item.name}}</label>
<input class="form-control" name="lastName" id="lastName" type="text" [formControlName]="item.name">
</div>
</form>
The items are fetched dynamically from the backend
Question: How can I populate controls dynamically in Angular reactive forms?