Problem,
I am using the same component for read/edit routines. async-validator
works perfectly with new entries. the problem starts if the user accidentally changes the value and tries to revert back to the saved value. my current code will run regardless and returns the value as existing. I want to pass more data along with control value so I can validate if that pair value exist already or not.
I am posting the relevant code,
this is my form control
,
patientEmail: new FormControl(
null,
[Validators.email, Validators.required],
FormControlValidator.createEmailAsyncValidator(
this.asyncValidatorService
),
),
my async validator creator class is,
export class FormControlValidator {
static createEmailAsyncValidator(asyncValidatorService: AsyncValidationService) {
return (control: AbstractControl) => {
if (!control.pristine) {
control.markAsPristine();
return asyncValidatorService
.validateEmailNotTaken(control)
.map((response: HttpResponse<boolean>) => {
return !response.body ? null : { taken: true };
});
}
return Observable.of(null);
};
}
and finally my service,
@Injectable()
export class AsyncValidationService {
constructor(private httpService: HttpClientService) {}
public validateEmailNotTaken(control: AbstractControl) {
return this.httpService.getRequest(
'PatientsRegistration/IsPatientEmailExist?email=' + control.value,
);
}
}
I want to be able to pass another param to my createEmailAsyncValidator
, something like another control value from the form if possible.