0Pricing
Angular Academy · Lesson

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

  1. The ControlValueAccessor Interface
  2. Writing and Registering Values
  3. Handling Disabled and Touched States
  4. Validation in Custom Controls
← Back to Angular Academy