I'm currently battling with Angular form array.
I have a form in which I add the fields dynamically.
I have created the form object:
this.otherDataForm = this.fb.group({
});
I add the dynamic fields like this:
addField(field: CustomFormField): void {
this.otherDataForm.addControl(id_campo, new FormControl('', Validators.required));
}
I loop through those fields:
<form *ngIf="selectedProfile" [formGroup]="otherDataForm">
<div class="mb-3" *ngFor="let field of fields; let i = index">
<label>{{field.descripcion}}</label>
<input class="form-control" [formControlName]="field.id_campo" type="number">
</div>
</form>
But I can't seem to get control of the errors of each field to show a validation message if the field is required.
Anyone can help me with this one? Maybe there is a better way to do this.
[formControlName]="field.id_campo"dynamic?id_campoand where is it set, please create a stackblitz to be able help you betterotherDataForm.get(field.id_campo)to get the formControl, sootherDataForm.get(field.id_campo).validgive you if is valid,otherDataForm.get(field.id_campo).errorsgive you the errors... even you can use in your input<input class="form-control" [formControl]="otherDataForm.get(field.id_campo)" type="number">