I have a form on which a user can dynamically add multiple input boxes for a user to add an email address.
On saving of the form, I am iterating through the list of controls, build up a JSON object and call an API to check in the DB to see if the email(s) address already exists.
If it exists, I would like to display an error message below the input indicating such .ie "email already in use".
I have a form array:
ts code
this.inviteForm = this.formBuilder.group({
invites: this.formBuilder.array([]),
alreadyUsedList: this.formBuilder.array([])
});
html
<div formArrayName="invites">
<div *ngFor="let unit of inviteForm.controls.invites.controls; let i=index" class="form-group">
<!-- The repeated alias template -->
<div [formGroupName]="i">
<input placeholder="" [type]="email" formControlName="invite" required/>
</div>
<div *ngIf="inviteForm.controls.alreadyUsedList.controls[i].controls.inUse.value == 'true'">
<div class="email-in-use">Email already in use</div>
</div>
</div>
</div>
In API response method I am attempting to set the value based on result array.
this.inviteForm.controls.alreadyUsedList.value[0].inUse = resData.Data.EmailCheck[0].AlreadyInUse
Dynamic add of email works.
API call works.
Response from API comes as expected.
PROBLEM:
Setting the value of the inUse assigns the value, however is does not result in the "Email already in use" ngIf condition being update and hence not displaying.
Any help would be appreciated.
SOLVED:
Everything was working as it should I was just not accessing the value in the array correctly in order to setValue.
this.inviteForm.controls.alreadyUsedList['controls'[0].controls['inUse'].setValue('true');
Thank you!