So, I'm creating a simple registration form with an async existing user validation, which executes a http request to my API that returns a boolean for existing user.
The component:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { UserTakenValidatorService } from '../../services/user-taken.validator.service';
@Component({
selector: 'app-signup',
templateUrl: './signup.component.html',
styleUrls: ['./signup.component.less'],
})
export class SignupComponent implements OnInit {
public signupForm: FormGroup;
public hide = true;
constructor(
private formBuilder: FormBuilder,
private checkUserTaken: UserTakenValidatorService,
) {}
ngOnInit(): void {
this.signupForm = this.formBuilder.group({
email: ['', [Validators.required, Validators.email]],
fullName: ['', [Validators.required, Validators.maxLength(40)]],
userName: [
'',
[Validators.required, Validators.maxLength(20)],
this.checkUserTaken.checkUserNameTaken(),
],
password: ['', [Validators.required, Validators.minLength(8), Validators.maxLength(20)]],
});
}
signup() {
console.log(this.signupForm.controls.userName.errors);
}
}
And this is the validator:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { AbstractControl } from '@angular/forms';
import { map } from 'rxjs/operators';
const API_URL = 'http://localhost:3000';
@Injectable({ providedIn: 'root' })
export class UserTakenValidatorService {
constructor(private http: HttpClient) {}
checkUserNameTaken() {
return (control: AbstractControl) => {
return this.verifyUsername(control.value)
.pipe(map((isTaken) => (isTaken ? { userNameTaken: true } : null)))
};
}
private verifyUsername(userName: string) {
return this.http.get<boolean>(`${API_URL}/user/exists/${userName}`);
}
}
This has been triggering an endless loop of requests when inputting an existing user (i.e. on true responses from API) that also don't add errors to signupForm.controls.userName.errors and that stops on the first false response.
P.S.: Dependencies are @angular/core/forms/common 11.0.0 and rxjs 6.6.0