What do I have to return in the customerNameValidator if the
async validation fails/succeeds that my 'customerName' FormControl is invalid?
this.customerForm = this.formBuilder.group({
customerName:
[this.newCustomerName, [Validators.minLength(2), Validators.required],[this.customerNameValidator.bind(this)]]
});
customerNameValidator(c: AbstractControl)
{
return this.service.customerExists(c.value,this.companyId).subscribe(response =>
{
if(response == true)
{
alert("true");
}
else
{
alert("false");
}
});
}
I implemented an reactive form with an AsyncValidatorFn on angular 6.1.1. and would like to share some of my learning
I found out that angular does not (automatically) update the form control for AsyncValidatorFn as it does for internal sync validators .
so, as per the "AsyncValidatorFn" interface spec, you have to "manually" update your form control in your implementation of
and then, you will check the control state in the html element
What I had implemented is an Username existence checking that might be very commonly found in an user sign-up process
below are the code excerpts:
the form control
the custom async validator and the helper functions
note that I input the control as a parameter into the "mapErr" function, and set the control by "c.setErrors(err);".
the "return err;" statement return "ValidationErrors" as per the "AsyncValidatorFn" interface spec.
the "gabriel.filter()"queries the backend with the extracted username; and returns 0, -100, -1 respectively for "ok", "duplicated", and "operation failed"
the control checking in html file
I also found out that the async validators are not trigger until sync validator are satisfied in one form control.
in my case, I also used the built-in Validators.pattern to define a minimum length of 3.(see above Username formControl definition)
the custom async validator never triggers as long as my input length is shorter than 3.
Rather than subscribing, you should map the observable to change the result of the returning stream, rather than reading from it.
Returning an object with a value that is true is how you should return the observable. You may be missing some important steps for async validators though, but because we don't gave all your code it's hard to say. Try checking out this article or this article for more information.