repeatValidatorFn
The repeatValidatorFn validator ensures two form fields contain matching values, typically used for password confirmation fields.
Description
A cross-field validator that compares two form controls within a FormGroup. When values don't match:
- Adds
fieldsDontMatcherror to second field - Preserves existing errors on second field
- Automatically clears error when values match
Parameters:
firstField: string - Name of first control to comparesecondField: string - Name of second control to compare
Returns: ValidatorFn - Validation function for FormGroup validators
How to use it
- Import the validator:
import { repeatValidatorFn } from '@geodome/gdk/common';
- Apply to FormGroup:
const form = new FormGroup(
{
password: new FormControl(''),
confirmPassword: new FormControl(''),
},
{
validators: repeatValidatorFn('password', 'confirmPassword'),
},
);
Template Error Handling
Display validation error using:
<div *ngIf="form.get('confirmPassword')?.hasError('fieldsDontMatch')"> Passwords do not match </div>
Key Features
- Exclusive to FormGroup controls
- Only modifies second field's errors
- Maintains existing validation errors
- Triggers on value changes
- Requires manual form state checks