You problem is the setTimeout in the validator:
static passwordValidator(control: AbstractControl): Promise<ValidationErrors | null> {
return new Promise((resolve, reject) => {
==> setTimeout(() => {
if (control.value === "1234") {
resolve({ passwordValidator: true })
}
resolve(null)
}, 2000);
});
}
setTimeout
bypasses Angulars ChangeDetection, therefore the view is not informed about the validation error.
Therefore you should always use Observables
in angular there is hardly a case where you need promises... and by using an observalbe you could delay the detection by piping
the timeout and the form validation should work properly
Update:
Your password validator should look like this:
export class CustomValidators {
static passwordValidator(control: AbstractControl): Observable<ValidationErrors | null> {
return of(control.value)
.pipe(
debounceTime(2000),
distinctUntilChanged(),
map(currentValue => currentValue === "1234" ? { passwordValidator: true } : null )
)
}
}
https://stackblitz.com/edit/angular-1su3cm