This is the archived documentation for Angular v6. Please visit angular.io to see documentation for the current version of Angular.

NG_VALIDATORS

An InjectionToken for registering additional synchronous validators used with AbstractControls.

const NG_VALIDATORS: InjectionToken<(Function | Validator)[]>;
      
      const NG_VALIDATORS: InjectionToken<(Function | Validator)[]>;
    

See also

Usage notes

Providing a custom validator

The following example registers a custom validator directive. Adding the validator to the existing collection of validators requires the multi: true option.

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