In the template file I'm iterating over an array of elements and for each element a separate row is inserted.
Each element has the same set of controls, but whenever I enter a wrong value inside one of the input fields the same error message appears on all other input controls. I need it to validate only the current input field.
Hope I explained it clearly. Below is my template code:
<tr *ngFor="let element of elements">
<td>
<input matInput
formControlName="elementNamePrefix"
[required]="controls.elementNamePrefix.required"
[placeholder]="controls.elementNamePrefix.displayName"
[type]="controls.elementNamePrefix.type">
<mat-error *ngIf="group.get('elementNamePrefix').hasError('maxlength')">
Max length is XY characters!
</mat-error>
</td>
</tr>
As per my understanding
parts
is aformArray
. Hence, you have to find the formGroup based on that index to display the error message in that particular control.Try this condition in
<mat-error>
container.TS :