Validation in Custom Controls
Provide validators from custom controls.
Self-Validating Controls
A custom control can carry its own validation logic by implementing the Validator interface and registering with NG_VALIDATORS, so consumers get validity for free.
The Validator Interface
Validator requires a validate method that returns ValidationErrors (an error map) or null when valid.
import { Validator, ValidationErrors, AbstractControl } from '@angular/forms';
export class RatingComponent implements Validator {
validate(control: AbstractControl): ValidationErrors | null {
return control.value > 0 ? null : { required: true };
}
}All lessons in this course
- The ControlValueAccessor Interface
- Writing and Registering Values
- Handling Disabled and Touched States
- Validation in Custom Controls