Adding new components with portal-FE-common
[portal.git] / portal-FE-common / src / app / shared / helpers / must-match-validator.ts
1 import { FormGroup } from '@angular/forms';
2
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];
8
9         if (matchingControl.errors && !matchingControl.errors.mustMatch) {
10             // return if another validator has already found an error on the matchingControl
11             return;
12         }
13
14         // set error on matchingControl if validation fails
15         if (control.value !== matchingControl.value) {
16             matchingControl.setErrors({ mustMatch: true });
17         } else {
18             matchingControl.setErrors(null);
19         }
20     }
21 }