I have apply a specific email validator, it works when I create the user, but if I try update any user, the email validator sends message email in use.
In another words its behaviour when I create, but I need when I update the user if id and email were equals, allow the user.
Is It possible through parameters?
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AbstractControl, AsyncValidator, FormControl, FormGroup, ValidationErrors, ValidatorFn } from '@angular/forms';
import { Observable, Subject } from 'rxjs';
import { map } from 'rxjs/operators';
import { url } from 'src/app/config/config';
@Injectable({
providedIn: 'root'
})
export class EmailValidatorService implements AsyncValidator {
constructor( private http: HttpClient ) { }
validate( control: AbstractControl): Observable<ValidationErrors | null> {
const email: string = control.value;
return this.http.get<any[]>( url +'email?email=' + email )
.pipe(
map( (resp: any) => {
let respuesta = null;
if( resp == null ) {
respuesta = null;
} else {
if (resp.user == null ) {
respuesta = null;
} else {
if( (email.localeCompare(resp.user.email) == 0) ) {
respuesta = { email_in_use: true} ;
} else {
respuesta = null;
}
}
}
return respuesta;
})
);
}
}