I am dynamically generating form fields using the ngFor
directive, with each field having a unique name
attribute. However, I am facing challenges when it comes to validating these fields.
Here is an example of my code:
<div *ngFor="let dimension of lstShippingDimensions; let i = index" class="dimension-item">
<input type="text" name="units-{{i}}" [(ngModel)]="dimension.units"
class="first-f" placeholder="# of units" required>
<input type="text" name="width-{{i}}" [(ngModel)]="dimension.width"
class="second-f" placeholder="W" required>
<input type="text" name="height-{{i}}" [(ngModel)]="dimension.height"
class="third-f" placeholder="H" required>
<input type="text" name="length-{{i}}" [(ngModel)]="dimension.length"
class="forth-f" placeholder="L" required>
<select class="five-f" name="unitType-{{i}}" [(ngModel)]="dimension.unitType"
required>
<option>inches</option>
<option>feet</option>
</select>
<div *ngIf="!units-{{i}}.valid && units-{{i}}.touched" class="text-danger text-left">
<small *ngIf="units-{{i}}.errors.required">Field is required.</small>
</div>
</div>