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

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

Validator

interface
stable

An interface implemented by classes that perform synchronous validation.

On this page

    arrow_upward_alt Back to the top

    API

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

    validate

    ValidationErrors | null

    Method that performs synchronous validation against the provided control.

    @paramcontrolAbstractControl<any, any, any>

    The control to validate against.

    @returnsValidationErrors | null

    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 validator

    The following example implements the Validator interface to create a validator directive with a custom error key.

    @Directive({
      selector: '[customValidator]',
      providers: [{provide: NG_VALIDATORS, useExisting: forwardRef(() => CustomValidatorDirective), multi: true}]
    })
    class CustomValidatorDirective implements Validator {
      validate(control: AbstractControl): ValidationErrors|null {
        return {'custom': true};
      }
    }
    Jump to details

    Web Proxy Viewer  |  New URL  |  Original Page