On this page
AsyncValidator
interface
An interface implemented by classes that perform asynchronous validation.
interface AsyncValidator extends Validator {
  validate(control: AbstractControl<any, any>): Promise<ValidationErrors | null> | Observable<ValidationErrors | null>
  // inherited from forms/Validator
  validate(control: AbstractControl<any, any>): ValidationErrors | null
  registerOnValidatorChange(fn: () => void)?: void
}
   Methods
| 
         
         validate()
          | 
      |||
|---|---|---|---|
Method that performs async validation against the provided control.  | 
      |||
        
          | 
      
control | 
            AbstractControl<any, any> | 
            The control to validate against.  | 
           
Returns
Promise<ValidationErrors | null> | Observable<ValidationErrors | null>: A promise or observable that resolves a map of validation errors if validation fails, otherwise null.
Usage notes
Provide a custom async validator directive
The following example implements the AsyncValidator interface to create an async validator directive with a custom error key.
import { of } from 'rxjs';
@Directive({
  selector: '[customAsyncValidator]',
  providers: [{provide: NG_ASYNC_VALIDATORS, useExisting: CustomAsyncValidatorDirective, multi:
true}]
})
class CustomAsyncValidatorDirective implements AsyncValidator {
  validate(control: AbstractControl): Observable<ValidationErrors|null> {
    return of({'custom': true});
  }
}
  © 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
 https://v14.angular.io/api/forms/AsyncValidator