[ Web Proxy ]
URL:
Viewing: https://angular.dev/api/forms/AsyncValidator [Back]  [Original]

AsyncValidator Angular Skip to main content
menu
close
more_horiz
menuAPI
@angular/forms

AsyncValidator

interface
stable

An interface implemented by classes that perform asynchronous validation.

Creating asynchronous validators

On this page

    arrow_upward_alt Back to the top

    API

        
          interface AsyncValidator extends Validator {  validate(control: AbstractControl<any, any, any>): Promise<ValidationErrors | null> | Observable<ValidationErrors | null>;  optional override registerOnValidatorChange(fn: () => void): void;}
        
        

    validate

    Promise<ValidationErrors | null> | Observable<ValidationErrors | null>

    Method that performs async validation against the provided control.

    @paramcontrolAbstractControl<any, any, any>

    The control to validate against.

    @returnsPromise<ValidationErrors | null> | Observable<ValidationErrors | null>

    A promise or observable that resolves a map of validation errors if validation fails, otherwise null.

    registerOnValidatorChange

    void

    Registers a callback function to call when the validator inputs change.

    @paramfn() => void

    The callback function

    @returnsvoid

    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});
      }
    }
    Jump to details

    Web Proxy Viewer  |  New URL  |  Original Page