On this page
Validator
interface
An interface implemented by classes that perform synchronous validation.
interface Validator {
  validate(control: AbstractControl): ValidationErrors | null
  registerOnValidatorChange(fn: () => void)?: void
}
   Child interfaces
Class implementations
Methods
| 
         
          validate() 
          | 
      |||
|---|---|---|---|
Method that performs synchronous validation against the provided control.  | 
      |||
        
          | 
      
control | 
            AbstractControl | 
            The control to validate against.  | 
           
Returns
ValidationErrors | null: A map of validation errors if validation fails, otherwise null.
| 
         
          registerOnValidatorChange() 
          | 
      |||
|---|---|---|---|
Registers a callback function to call when the validator inputs change.  | 
      |||
        
          | 
      
fn | 
            () => void | 
            The callback function  | 
           
Returns
void
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: CustomValidatorDirective, multi: true}]
})
class CustomValidatorDirective implements Validator {
  validate(control: AbstractControl): ValidationErrors|null {
    return {'custom': true};
  }
}
  © 2010–2021 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
 https://v11.angular.io/api/forms/Validator