New to Angular and trying to add a custom email validator that will go to my server and check if the email address is already in use.
But can't seem to get the error message displaying on form. Here is how I'm testing it before I attempt to hit the server.
FYI - the "emailMatchValidator()" is called but I can't get it to display any kind of error message on the form!
Maybe I should try something other than .touched!
ngOnInit() {
this.createRegisterForm();
}
createRegisterForm() {
this.registerForm = this.fb.group({
gender: ['male'],
email: ['', [Validators.required, Validators.email]],
username: ['', [Validators.required, Validators.minLength(4), Validators.maxLength(10)]],
knownAs: ['', Validators.required],
dateOfBirth: [null, Validators.required],
city: ['', Validators.required],
country: ['', Validators.required],
password: ['', [Validators.required, Validators.minLength(4), Validators.maxLength(8)]],
confirmPassword: ['', Validators.required]
}, {
validator: [this.passwordMatchValidator, this.emailMatchValidator]
});
}
emailMatchValidator(g: FormGroup) {
return g.get('email').value !== '[email protected]' ? null : {
emailExists: true
};
}
<div class="form-group">
<input type="email"
[ngClass]="{'is-invalid': registerForm.get('email').errors && registerForm.get('email').touched}
|| registerForm.get('email').touched && registerForm.hasError('emailExists')"
class="form-control"
formControlName="email"
placeholder="Email">
<div class="invalid-feedback" *ngIf="registerForm.get('email').touched && registerForm.get('email').hasError('required')">
Email is required
</div>
<div class="invalid-feedback" *ngIf="registerForm.get('email').touched && registerForm.get('email').hasError('email')">
Invalid email address
</div>
<div class="invalid-feedback" *ngIf="registerForm.get('email').touched && registerForm.get('email').hasError('emailExists')">
Email address already in use
</div>
</div>