In my current application I am trying to build a form that allows users to change their email or username. I have a validator on the form fields so users cannot have the same username or password as other users. I prefill the field with users' current username and email, however, if I click into that field delete one character and then reapply it, I trigger warning text. How can I make the validator not raise an error if the current user enters their own username?
import { Injectable } from '@angular/core';
import { FormControl, AbstractControl } from '@angular/forms';
import { UserinfoService } from 'src/services&Validtors/userinfo.service';
import { map } from 'rxjs/operators';
import { HttpClient, HttpParams } from '@angular/common/http';
@Injectable()
export class EmailValidator {
constructor(private http:HttpClient) { }
validateEmailNotTaken(control: AbstractControl) {
return this.checkEmailNotTaken(control.value).pipe(
map(res => {
return res ? null : { emailTaken: true };
})
);
}
checkEmailNotTaken(email: string) {
return this.http.get("http://127.0.0.1:8000/accounts/UserDataEmail/").pipe(
map((emailList: Array<any>) =>
emailList.filter(user => user.email === email)
),
map(email => !email.length)
);
}
}