{ "id": "api/forms/AsyncValidator", "title": "AsyncValidator", "contents": "\n\n
\n
\n
\n \n API > @angular/forms\n
\n \n
\n \n
\n

AsyncValidatorlink

\n \n \n \n \n \n
\n \n \n\n
\n \n
\n

An interface implemented by classes that perform asynchronous validation.

\n\n \n
\n \n \n
\n\ninterface AsyncValidator extends Validator {\n validate(control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null>\n\n // inherited from forms/Validator\n validate(control: AbstractControl): ValidationErrors | null\n registerOnValidatorChange(fn: () => void)?: void\n}\n\n\n \n \n\n\n \n \n\n
\n\n \n\n \n \n \n\n \n\n
\n

Methodslink

\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n\n \n \n
\n
\n

\n validate()\n \n link

\n \n
\n
\n

Method that performs async validation against the provided control.

\n\n
\n
\n \n\n validate(control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null>\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n control\n AbstractControl\n

The control to validate against.

\n\n
\n\n \n
Returns
\n

Promise<ValidationErrors | null> | Observable<ValidationErrors | null>: A promise or observable that resolves a map of validation errors\nif validation fails, otherwise null.

\n\n \n\n\n \n\n \n
\n
\n\n \n
\n\n\n \n
\n

Usage noteslink

\n

Provide a custom async validator directivelink

\n

The following example implements the AsyncValidator interface to create an\nasync validator directive with a custom error key.

\n\nimport { of } from 'rxjs';\n\n@Directive({\n selector: '[customAsyncValidator]',\n providers: [{provide: NG_ASYNC_VALIDATORS, useExisting: CustomAsyncValidatorDirective, multi:\ntrue}]\n})\nclass CustomAsyncValidatorDirective implements AsyncValidator {\n validate(control: AbstractControl): Observable<ValidationErrors|null> {\n return of({'custom': true});\n }\n}\n\n\n
\n\n\n\n
\n
\n\n\n" }