I have a form where I can check a user's email address against the DB to see if it already exists. If it does exist they are redirected to a login page and if it does not exist they continue the journey.
I would only like to only call the emailAlreadyExists() function if the user has an active link. If activeLink == true. I cannot seem to work out how to place the condition within my reactive form or within the AsyncValidatorFn.
I have tried placing an if/else statement within AsyncValidatorFn and have also tried placing bind(this) & using of (rxjs) within the reactive form but neither works without errors.
ReactiveForm
this.firstFormGroup = this._formBuilder.group({
email: [this.user.email, {
validators: [Validators.required, Validators.email],
asyncValidators: [this.emailAlreadyExists()],
updateOn: 'blur'
}]
})
AsyncValidatorFn
public emailAlreadyExists(): AsyncValidatorFn {
return (control: AbstractControl): Observable<ValidationErrors | null> => {
return this.userService.emailAlreadyExists(new RequestEmailExists({
email: control.value,
identifier: this.user.identifier })).pipe(
map(res => {
return res ? { alreadyExistsValidator: true } : null;
})
);
};
}