diff options
Diffstat (limited to 'portal-FE-common/src/app/shared/helpers')
-rw-r--r-- | portal-FE-common/src/app/shared/helpers/must-match-validator.ts | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/portal-FE-common/src/app/shared/helpers/must-match-validator.ts b/portal-FE-common/src/app/shared/helpers/must-match-validator.ts new file mode 100644 index 00000000..70e9e1a4 --- /dev/null +++ b/portal-FE-common/src/app/shared/helpers/must-match-validator.ts @@ -0,0 +1,21 @@ +import { FormGroup } from '@angular/forms'; + +// custom validator to check that two fields match +export function MustMatch(controlName: string, matchingControlName: string) { + return (formGroup: FormGroup) => { + const control = formGroup.controls[controlName]; + const matchingControl = formGroup.controls[matchingControlName]; + + if (matchingControl.errors && !matchingControl.errors.mustMatch) { + // return if another validator has already found an error on the matchingControl + return; + } + + // set error on matchingControl if validation fails + if (control.value !== matchingControl.value) { + matchingControl.setErrors({ mustMatch: true }); + } else { + matchingControl.setErrors(null); + } + } +}
\ No newline at end of file |