I have a basic form that checks if an email address exists as an Async Validator.
ngOnInit(): void {
this.form = new FormGroup({
email: new FormControl('',
[
Validators.required,
Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$')
],
[
pendingPortfolioUserRequestValidator(this.portfolioInviteService, this.data.portfolioId)
])
});
}
The Async Validator pendingPortfolioUserRequestValidator looks like this
export function pendingPortfolioUserRequestValidator(portfolioInviteService: PortfolioInviteService,
portfolioId: number): AsyncValidatorFn {
return (control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> => {
return portfolioInviteService.checkUserIsPending(portfolioId, control.value).pipe(map((email) => {
return (email) ? {'pendingUserExists': true} : null;
}));
};
}
The line of code that determines whether the email exists or not (return (email) ? {'pendingUserExists': true} : null;) is getting hit, but when I check the errors collection, it's giving null as the value.
I have also tried to use a promise, but nothing seems to work.