Answer the question
In order to leave comments, you need to log in
Angular 8: When I try to inject a validator into a component's constructor, Angular redirects me to the main page without throwing any errors. Why?
The problem seems to be quite simple, but I can't find a solution. There is a registration-page.component.ts component that simply displays a form with one field - email
import { Component, OnInit } from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
import {EmailExistsValidator} from '../shared/services/my.validators';
@Component({
selector: 'app-registration-page',
templateUrl: './registration-page.component.html',
styleUrls: ['./registration-page.component.css']
})
export class RegistrationPageComponent implements OnInit {
form: FormGroup
constructor() { }
ngOnInit() {
this.form = new FormGroup(
{
email: new FormControl(null, [Validators.required, /*this.emailExistsValidator.validate.bind(this.emailExistsValidator)*/])
});
}
}
import {EmailService} from './email.service';
import {catchError, map} from 'rxjs/operators';
import {AbstractControl, AsyncValidator, ValidationErrors} from '@angular/forms';
import {Observable} from 'rxjs';
import {Injectable} from '@angular/core';
@Injectable({ providedIn: 'root' })
export class EmailExistsValidator implements AsyncValidator {
constructor(private emailService: EmailService) {}
validate(
ctrl: AbstractControl
): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> {
return this.emailService.Is_email_exists(ctrl.value).pipe(
map(isTaken => (isTaken ? { uniqueAlterEgo: true } : null)),
catchError(() => null)
);
}
}
constructor() { }
constructor(private emailExistsValidator: EmailExistsValidator) { }
Answer the question
In order to leave comments, you need to log in
I don’t know why you are being redirected, apparently the error is being intercepted somewhere. ngOnInit in the component should throw an exception for you.
At first glance, I see:
1. async validators are passed in the third parameter of the FormControl, and you have it in the second, along with simple validators.
Further, I don’t see the point of making a validator through a service that implements AsyncValidator, in the examples it is used to create directives. In your example, a validator will be passed to the directive using the NG_ASYNC_VALIDATORS token.
You can probably use AsyncValidator for a component (of the type that implements ControlValueAccessor), but you will also need to provide it by specifying the token (but this is not accurate). Usually there is simply no need for this.
For simple async validation, you can use a validator factory function that takes dependencies from a component.
Like this https://ng-run.com/edit/DDaBjbxR39I7DIImTg0n
Yes, and more. Your validator will make requests for each character entered in the form. To avoid this, you need to specify updateOn.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question