In my Angular8 project with Bootstrap, I have created input fields dynamically from an array using a loop. When there is a validation error for the email or fax number input, the error is displayed. However, I noticed that if there is an error in the first row's email field and then an error in the second row's fax field, only the second error is shown. Additionally, if the email field in the third row is initially incorrect but then corrected, the validation error for the first row also disappears.
My objective is to display validation errors for all rows if there is an issue with either the fax or email field in any of them.
HTML:
<input type="text" class="form-control" placeholder="Email" name="Recepient"
formControlName="recipients" *ngIf="data.value.deliveryMethodId == 178"
(focusout)="validationErrorOnFocusOut('emailvalid',data)"
[ngClass]="{ 'is-invalid': emailsubmitted && data.controls.recipients.errors}"
pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$" autocomplete="off">
<div *ngIf="(emailsubmitted && data.controls.recipients.errors)" class="invalid-feedback">
<div *ngIf="(emailsubmitted && data.controls.recipients.errors)">
Please enter valid email</div>
</div>
<input type='text' prefix="+1 " mask=" (000) 000-0000" class="form-control"
placeholder="Recepient" name="Recepient" formControlName="recipients"
*ngIf="data.value.deliveryMethodId == 179" maxLength="18"
(focusout)="validationErrorOnFocusOut('fax',data)"
autocomplete="off"
[ngClass]="{ 'is-invalid':faxsubmitted && data.controls.recipients.errors.mask}">
<div *ngIf="faxsubmitted && data.controls.recipients.errors.mask" class="invalid-feedback">
<div *ngIf="faxsubmitted && data.controls.recipients.errors.mask">Please enter valid fax number
</div>
</div>
TS:
public validationErrorOnFocusOut(name, data) {
if (name == "emailvalid") {
if (data.controls.recipients.status === "VALID") {
this.emailsubmitted = false;
} else {
this.emailsubmitted = true;
}
if (
data.controls.recipients.value === "" ||
data.controls.recipients.value === null
) {
this.emailsubmitted = false;
}
} else if (name == "fax") {
if (data.controls.recipients.status === "VALID") {
this.faxsubmitted = false;
} else {
this.faxsubmitted = true;
}
if (
data.controls.recipients.value === "" ||
data.controls.recipients.value === null
) {
this.faxsubmitted = false;
}
}
}