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

Validatorlink

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

An interface implemented by classes that perform synchronous validation.

\n\n \n
\n \n \n
\n\ninterface Validator {\n validate(control: AbstractControl): ValidationErrors | null\n registerOnValidatorChange(fn: () => void)?: void\n}\n\n\n \n \n
\n

Child interfaceslink

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

Class implementationslink

\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 synchronous validation against the provided control.

\n\n
\n
\n \n\n validate(control: AbstractControl): 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

ValidationErrors | null: A map of validation errors if validation fails,\notherwise null.

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

\n registerOnValidatorChange()\n \n link

\n \n
\n
\n

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

\n\n
\n
\n \n\n registerOnValidatorChange(fn: () => void)?: void\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n fn\n () => void\n

The callback function

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

void

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

Usage noteslink

\n

Provide a custom validatorlink

\n

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

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