1 import { FormGroup } from '@angular/forms';
3 // custom validator to check that two fields match
4 export function MustMatch(controlName: string, matchingControlName: string) {
5 return (formGroup: FormGroup) => {
6 const control = formGroup.controls[controlName];
7 const matchingControl = formGroup.controls[matchingControlName];
9 if (matchingControl.errors && !matchingControl.errors.mustMatch) {
10 // return if another validator has already found an error on the matchingControl
14 // set error on matchingControl if validation fails
15 if (control.value !== matchingControl.value) {
16 matchingControl.setErrors({ mustMatch: true });
18 matchingControl.setErrors(null);