I'm working on converting a RazorPages application to Blazor server rendered, the application is really just a UI, all data access/manipulation is done through an API. On CRUD operations the API will return a collection of error objects in a standardized format.
I have code in the RazorPages application to convert the collection of errors into items in the ModelStateDictionary and it works as expected. Messages display, and I can make changes and resubmit the form.
I've added similar code in the Blazor application to add to the EditContext, but I'm struggling to figure out how to clear the validation messages that were added by my extension method. I've been looking at this question and all the solutions suggested, but none seem to work for me at all: How to reset custom validation errors when using editform in blazor razor page
This is the method that actually does the manipulation of the EditContext:
private static void AddErrors(EditContext editContext, Dictionary<string, List<string>> errorDictionary)
{
if (errorDictionary != null && errorDictionary.Any())
{
var validationMessageStore = new ValidationMessageStore(editContext);
foreach (var error in errorDictionary)
{
validationMessageStore.Add(editContext.Field(error.Key), error.Value);
}
editContext.NotifyValidationStateChanged();
}
}
Here's the form definition:
<EditForm class="form-signin" OnValidSubmit="OnSubmit" Model="loginModel" Context="CurrentEditContext">
<DataAnnotationsValidator />
<SummaryOnlyValidationSummary />
<div class="form-group">
<InputText id="inputUsername" class="form-control" @bind-Value="loginModel.Username" autofocus placeholder="Username" @onkeyup="@(q => ResetValidation(CurrentEditContext))" />
<ValidationMessage For="@(() => loginModel.Username)" />
</div>
<div class="form-group">
<InputText type="password" id="inputPassword" class="form-control" placeholder="Password" @bind-Value="loginModel.Password" />
<ValidationMessage For="@(() => loginModel.Password)" />
</div>
<div class="form-group">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</div>
</EditForm>
This is the method signature for when the form is submitted, and the editContext passed in appears accurate based on the validation messages it contains (messages from the Required attributes on the model properties are correct):
protected async Task OnSubmit(EditContext editContext)
SummaryOnlyValidationSummary is a custom component that works just like the out of the box ValidationSummary except it excludes any message that is already displayed by the DataAnnotationsValidator
Am I adding the messages the wrong way? If so, is there any good guidelines of where I went wrong? If not, does anyone know how to properly clear or reset the validation messages? I've tried to always submit the form, not only when it's valid, but even forcing editContext.Validate() doesn't clear the validation message my code added. If it helps at all, in ResetValidation I create a new ValidationMessageStore instance off the current editContext, and it doesn't contain any messages (not sure what the expected behavior is though).