Both mat-error show when only one error appeared.
I'm trying to make custom validators with mat-error. Both input for email and confirm password are red when each of them has a true value for hasError('').
I think my MyErrorStateMatcher class logic is wrong somehow. Please help! I have tried anything I could. Thank you in advance!
As you can see in the image. When confirmPassword throws an error, the email field is also red.
My ErrorStateMatcher:
export class MyErrorStateMatcher implements ErrorStateMatcher {
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
const invalidCtrl = !!(control && control.invalid && (control.dirty || control.touched));
const invalidParent = !!(control && (control.parent.hasError('notTheSame') || control.parent.hasError('emailUsed')));
return ((invalidCtrl || invalidParent));
}
}
HTML: (focus on email and confimPassword)
<form [formGroup]="signupForm" (ngSubmit)="signup(signupForm)">
<mat-form-field style="width: 100%;">
<input matInput formControlName="email" placeholder="Email address" type="email" [errorStateMatcher]="matcher" required>
<mat-error *ngIf="signupForm.controls.email.hasError('required')">Email required!</mat-error>
<mat-error *ngIf="signupForm.controls.email.hasError('email')">Email invalid!</mat-error>
<mat-error *ngIf="signupForm.hasError('emailUsed')">This email already exists!</mat-error>
</mat-form-field>
<mat-form-field style="width: 100%;">
<input matInput formControlName="username" placeholder="User name" (blur)="signupForm.value.username != null ? isValidUsername(signupForm.value.username) : ''" required />
<mat-error>Please enter your new username!</mat-error>
<mat-error *ngIf="usernameInvalid">Username already exists!</mat-error>
</mat-form-field>
<mat-form-field style="width: 100%;">
<input matInput formControlName="password" placeholder="New password" [type]="show ? 'text' : 'password'" required />
<mat-icon matSuffix (click)="show = !show" style="cursor: pointer;">{{show ? 'visibility' : 'visibility_off'}}</mat-icon>
<mat-error>Please enter your password!</mat-error>
</mat-form-field>
<mat-form-field style="width: 100%;">
<input matInput formControlName="confirmPassword" placeholder="Confirm password" type="password" [errorStateMatcher]="matcher" required>
<mat-error *ngIf="signupForm.controls.confirmPassword.hasError('required')">Please confirm your password!</mat-error>
<mat-error *ngIf="signupForm.hasError('notTheSame') && signupForm.value.confirmPassword != ''">Password is not the same!</mat-error>
</mat-form-field>
<br>
<button mat-raised-button class="sessionBtn" color="primary" [disabled]="signupForm.invalid">Submit!</button>
</form>
TS:
signupForm = new FormGroup({
firstName: new FormControl(),
lastName: new FormControl(),
email: new FormControl('', [
Validators.required,
Validators.email
]),
username: new FormControl(),
password: new FormControl('', [
Validators.required
]),
confirmPassword: new FormControl('', [
Validators.required
])
}, { validators: [this.checkPassword, this.checkExistingEmail] });
matcher = new MyErrorStateMatcher();
/////////Custom validator////////
checkPassword(signupForm: FormGroup) {
let password = signupForm.value.password;
let confirmPassword = signupForm.value.confirmPassword;
return password === confirmPassword ? null : { notTheSame: true };
}
checkExistingEmail(signupForm: FormGroup) {
let inputEmail = signupForm.value.email;
let dbEmail = "test@test.com";
return inputEmail !== dbEmail ? null: { emailUsed: true };
}
Error happend with input email and input confirmPassword, both have [errorStateMatcher]="matcher"