This is the custom validator I have atm :
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) {
return { mustMatch: true };
} else {
return null;
}
}
}
I have also made a formGroup in the ts file that has the 2 values newPass and confirmPass This is the code :
import { Component, OnInit } from '@angular/core';
import {FormGroup, FormControl, Validator, Validators} from "@angular/forms";
// custom validator to check if password confirmation matches
import { MustMatch } from "../../customValidators/mustMatch";
@Component({
selector: 'app-change-password',
templateUrl: './change-password.component.html',
styleUrls: ['./change-password.component.scss']
})
export class ChangePasswordComponent implements OnInit {
//formGroup met 2 formControlls, 1 voor gebruiekrsnaam en 1 voor wachtwoord
changePasswordForm = new FormGroup({
currentPass: new FormControl('', Validators.compose([Validators.required])),
newPass: new FormControl('', Validators.compose([Validators.required])),
confirmPass: new FormControl('',Validators.compose([Validators.required,])),
}, [MustMatch('newPass', 'confirmPass')]);
get f() { return this.changePasswordForm.controls; }
constructor() { }
ngOnInit() {
}
}
Now I'm trying to use *ngIf to check if the custom validator gives an error, but I'm also checking on required. I'm using a span aswell to make sure the error only validates if the input field returns touched or dirty. Here is the code :
<div class="fullwidthContainer">
<div class="container">
<div class="login-container">
<div class="form-control">
<form [formGroup]="changePasswordForm" class="myForm">
<div class="form-group">
<input type="password" placeholder="Current Password" formControlName="currentPass">
</div>
<div class="form-group">
<input type="password" placeholder="New Password" formControlName="newPass">
</div>
<div class="form-group">
<input type="password" placeholder="Confirm New Password" formControlName="confirmPass">
<span class="help-block" *ngIf="changePasswordForm.get('confirmPass').errors &&
(changePasswordForm.get('confirmPass').touched || changePasswordForm.get('confirmPass').dirty)">
<div *ngIf="f.confirmPass.errors.required">Confirm Password is required</div>
<div *ngIf="f.confirmPass.errors.MustMatch">Passwords must match</div>
</span>
</div>
</form>
</div>
</div>
<app-button [buttonText]="'Confirm'" [routerLink]="['../userPage']"
></app-button>
</div>
</div>
The problem now is that only the required validator works, not the custom MustMatch validator.
<div *ngIf="changePasswordForm.hasError'(MustMatch')">Passwords must match</div>. Don't access errors directly: it can be null. - JB Nizet