I have a standard change password page, so you input your old password, then the new one and finally repeat the new one .
On the "new password" I have multiple custom validators, and I need one more to check if the "new password" is equal to the "old password".
But of course I cannot do that from FormControl because I don't have access to the "old password" FormControl, so I need to do the validation on FormGroup.
But what happens is, when it validates this part on FormGroup, then suddenly all validators on the "new password" FormControl are triggered!!
Is there a reason?
this.myForm = this.formBuilder.group(
{
oldPassword: [
"",
{
validators: [Validators.required],
],
newPassword: [
"",
Validators.compose([
Validators.required,
CustomValidators.patternValidator(/\d/, { hasNumber: true }),
CustomValidators.patternValidator(/[A-Z]/, {
hasCapitalCase: true,
}),
CustomValidators.patternValidator(/[a-z]/, { hasSmallCase: true }),
CustomValidators.patternValidator(
/[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/,
{ hasSpecialCharacters: true }
),
Validators.minLength(8),
]),
],
confirmNewPassword: ["", Validators.compose([Validators.required])],
},
{ validators: [CustomValidators.passwordMatchValidator] }
);
}
my Custom passwordMatchValidator:
static passwordMatchValidator(control: AbstractControl) {
const password: string = control.get("newPassword").value;
const confirmPassword: string = control.get("confirmNewPassword").value;
const oldPassword: string = control.get("oldPassword").value;
if (password !== confirmPassword) {
control.get("confirmNewPassword").setErrors({ NoPassswordMatch: true });
}
if (password === oldPassword) {
control.get("newPassword").setErrors({ OldPasswordMatch: true });
}
}
so basically, if this FormGroup validator triggers the "OldPasswordMatch" error, then suddenly, all the specific Validators on newPassword FormControl will get triggered, why is that?
I cannot put this validation on the FormControl "new password" because it has no access to the FormControl "old password"
updateOnoption. regardless of what you're doing at the form group level. if a form control is marked as required, if it has no value, it will be marked as invalid and as having a required error no matter what - bryan60