I have this form group defined
get createItem(): FormGroup {
return this.formBuilder.group({
name: ['', Validators.required],
email: ['', Validators.required],
mobile: ['', Validators.required],
});
}
Generating fields like this
<form [formGroup]="dynamicFormGroup" (ngSubmit)="onSubmit()">
<div class="row" formArrayName="address" *ngFor="let fields of AddressInfo.controls; let i = index">
<ng-container [formGroupName]="i">
<div class="form-group col-md-12">
<div class="each_content">
<label class="labelTitle">Name <span class="validation-error">*</span></label>
<input type="text" class="form-control height-reset" placeholder="Enter Name" name="name" formControlName="name" />
<div
*ngIf="dynamicFormGroup.get('name').invalid && (dynamicFormGroup.get('name').dirty || dynamicFormGroup.get('name').touched)">
<small class="validation-error">Please provide a title.</small>
</div>
However, the validation is not working, any solution thanks.
I edited your StackBlitz a bit and made some minor changes. Please check and see if it works for you. I introduced a
hasErrorfunction, which takes the according input and checks the validity.The template would look like this:
The
dynamicFormGroup.get('name').invalidin your code would not return anything, since you are trying to get a formGroup inside of the form, but you would need a specific formGroup inside of the nested formArray.dynamicFormGroup --> adresses --> specific name input
By passing the index and the formControlName to the hasError function this should work.