Injecting Validators

In this lesson, we'll learn how to use our custom validators using dependency injection.

The validator we created must be applied to form groups because the validate function is expecting a form group. This is because we want access to both passwords. By applying the validator to the form group, we’ll have access to all form controls.

Let’s update the validate function to check if the passwords match.

Press + to interact
import { Validator, FormGroup } from '@angular/forms';
export class Match implements Validator {
validate(formGroup: FormGroup) {
const { password, passwordConfirmation } = formGroup.value;
if (password === passwordConfirmation) {
return null;
}
return { mismatch: true };
}
}

In the example above, we’re grabbing the values by destructuring the password and ...