Replies: 1 comment
-
Ok, I made some progress based on this blog post : https://www.learmoreseekmore.com/2021/01/blazor-server-forms-validator-component.html I created this component public class ServerSideValidator : ComponentBase
{
private ValidationMessageStore _messageStore;
[CascadingParameter]
public EditContext CurrentEditContext { get; set; } = null!;
protected override void OnInitialized()
{
if (CurrentEditContext == null)
{
throw new InvalidOperationException("To use validator component your razor page should have the edit component");
}
_messageStore = new ValidationMessageStore(CurrentEditContext);
CurrentEditContext.OnValidationRequested += (s, e) => _messageStore.Clear();
CurrentEditContext.OnFieldChanged += (s, e) => _messageStore.Clear(e.FieldIdentifier);
}
public void DisplayErrors(Dictionary<string, List<string>> errors)
{
foreach (var error in errors)
{
_messageStore.Add(CurrentEditContext.Field(error.Key), error.Value);
}
CurrentEditContext.NotifyValidationStateChanged();
}
} I have two validators now : <EditForm Model="@Data" OnValidSubmit="@HandleValidSubmit">
<FluentValidationValidator DisableAssemblyScanning="@true" />
<ServerSideValidator @ref="serverSideValidator" />
</EditForm> And I can add my custom error like this protected async Task HandleValidSubmit()
{
try
{
SavePersonInDb(Person);
}
catch (NotUniqueException e)
{
var errors = new Dictionary<string, List<string>>();
errors.Add(nameof(Data.Description), new List<string> { "Testing description" });
serverSideValidator.DisplayErrors(errors);
}
} Now it works, but I'm wondering if there is a more elegant way to do it ? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I successfully plugged FluentValidation in a simple Blazor application and everything works fine for basic validation.
Imagine we have this classes
When I submit my form FluentValidation is doing it's job if the Name is empty.
My problem is that I have later another rule in my code for exemple Name must be unique in a database.
I'm not sure how to add an error to the ModelState and return to the form page.
Beta Was this translation helpful? Give feedback.
All reactions