Reputation: 1760
In my reactive form I have two password fields. Two match the two field's value I have created a custom validator and then assign that validator in form group in ts file. The code is below:
this.registerForm = new FormGroup({
title: new FormControl("", Validators.required),
firstName: new FormControl("", Validators.required),
lastName: new FormControl("", Validators.required),
password: new FormControl("", [Validators.required, Validators.minLength(6)]),
confirmPassword: new FormControl("", Validators.required),
acceptTerms: new FormControl("", Validators.required)
},
{
validator: MustMatch('password', 'confirmPassword')
});
Must Match Validator code:
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.mustMatch11111) {
// 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({ mustMatch11111: true });
} else {
matchingControl.setErrors(null);
}
}
}
But MustMatch is showing error which is below:
Argument of type '{ validator: (formGroup: FormGroup) => void; }' is not assignable to parameter of type 'ValidatorFn | ValidatorFn[] | AbstractControlOptions'. Object literal may only specify known properties, and 'validator' does not exist in type 'ValidatorFn | ValidatorFn[] | AbstractControlOptions'.
66 validator: MustMatch('password', 'confirmPassword')
As I am new to angular, can anyone please help me to solve the problem.
Upvotes: 0
Views: 388