My Async Validator looks like this:
asyncValidator(service:ApiCallsService):AsyncValidatorFn{
return (control:FormControl):Promise<ValidationErrors | null> | Observable<ValidationErrors | null> =>{
let timer$ = timer(2000);
return timer$.pipe(
take(1),
switchMap(()=> {
let videoId = service.checkUrl(control.value);
return service.getVideoDescription(videoId).toPromise().then((val:any)=>{
return (!val.id) ? {"invalidUrl": true} : null;
})
})
)
}
}
The problem with my Async Validator is that my FormControls that are added into my FormArray do not pick up on the current 'status' of themselves until they are blurred.
This is my FormArray and my FormControl inside it:
<div class="url-con" formArrayName="urls" >
<div *ngFor="let url of urls.controls; let i=index" class="url-input-con">
<input minLength="5" placeholder="Video Url" class="url-input" [formControlName]="i">
<div class="url-pending" *ngIf="urls.controls[i].pending && !urls.controls[i].valid">Validating...</div>
</div>
</div>
The div with the class "url-pending" appears, and then it doesn't disappear - even though the FormControl it depends upon is validated by the backend - until the user blurs the FormControl that the div depends on.
The only other stackoverflow question that is similar to this is this link. I could not fully understand the instructions of that link, and an additional complication I had compared to the poster of the link is that I have an icon in my form shaped as a plus sign so that the user can add more FormControls to the FormArray. I couldn't figure out how to add directives to the FormControls that the user added by interacting with the form.
I will answer my own question because I figured out how to solve this question, but I solved it in a 'hackish' way. If anybody else knows a better answer to this, please reply.