I have a Blazor component representing a form, that needs to perform some expensive validation before submitting, to ensure the uniqueness of a data point on a server. I tried to use this documentation as inspiration: https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1
It seems like better documentation for this is on the Microsoft documentation roadmap: https://github.com/dotnet/AspNetCore.Docs/issues/17377
My component looks like this:
<EditForm Model="@form" OnValidSubmit="@Submit">
<InputText @bind-Value="form.DataPoint" />
<ValidationMessage For="() => form.DataPoint" />
<button type="submit">Go!</button>
</EditForm>
@code {
private Form form = new Form();
private EditContext editContext;
protected override void OnInitialized()
{
editContext = new EditContext(form);
}
private async Task Submit()
{
var isValid = editContext.Validate() && await ServerValidate(editContext);
if (isValid)
{
// do stuff
}
}
private async Task<bool> ServerValidate(EditContext editContext)
{
var form = (Form)editContext.Model;
var validationErrors = new ValidationMessageStore(editContext);
var isDataPointCollision = await SomeService.CheckUniqueness(form.DataPoint);
if (isDataPointCollision)
{
var field = new FieldIdentifier(form, nameof(Form.DataPoint));
validationErrors.Add(field, "This data point already exists, please type a different one");
editContext.NotifyValidationStateChanged();
return false;
}
return true;
}
}
My validation code works properly, detects the collision, and prevents submission of the form. However, the UI does not update as expected with the <ValidationMessage [...] /> component. No UI update occurs at all, and no validation messages are displayed. I've also tried this component:
<ValidationSummary Model="@form" />
to no avail.
There are no errors either on the server, or in the client-side JS.
Am I barking up the wrong tree with this approach, or have I missed a connection somewhere? Is there a better way to accomplish what I'm looking to do?