[Fixed] Get invalid Validators in formControlName

Issue

For example I have Validators below

username: ['', [Validators.required, Validators.pattern(...)], this.validateUsernameNotTaken.bind(this)]

If this username is invalid I want know if it`s caused by async validator or not. Also I want get any information about invalid not-async validators.

How can I do this?

Solution

A ValidatorFn is a function that takes your FormControl and returns a ValidationErrors or null if the value is valid :

interface ValidatorFn {
  (control: AbstractControl): ValidationErrors | null
}

Similarly, a AsyncValidatorFn is a function that takes your FormControl and returns an Observable<ValidationErrors | null> (or a Promise)

interface AsyncValidatorFn {
  (control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null>
}

In both cases, they produce some ValidationErrors that you can get in FormControl.errors

Leave a Reply

(*) Required, Your email will not be published