I have a use case where, when the user enters an email in a form, I need to verify it's unique. This is for registering as a new user and every user email in our app must be unique.
The call to do this takes time as it's hitting a database. Therefore I'd like it to be async. So would the following work and how do I accomplish the below problematic steps.
What if I use CustomValidation instead of ValidationAttribute. This works great for synchronous complex validation. But making it async...
<EditForm EditContext="_editContext" OnValidSubmit="HandleValidSubmitAsync" OnInvalidSubmit="HandleInvalidSubmitAsync" Context="EditFormContext"><DataAnnotationsValidator /><CustomValidation @ref="_customValidation" />When it is called for validation it kicks off a background Task and returns an error of "checking email..."
Now here's where I don't know exactly what to do. When the task completes:
- How do I update the CustomValidation object, either saying the email is valid or updating the message to now say the email is a duplicate?
- If I can simply update _customValidation.ClearErrors()/DisplayErrors(), can I call it in the thread the task completes in? Or do I need to do something similar to WinForms where I invoke a method that will be called in the main/UI thread?
- How do I then tell the form that the CustomValidation object has changed, possibly now allowing a submit?
If all I can do when the task completes is update an object and then force the form to re-validate the email, I can make that work. But in that case I still have #3 above, how do I make that happen?