Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelState.AddModelError in Blazor Form #14524

Closed
ryanelian opened this issue Sep 27, 2019 · 2 comments
Closed

ModelState.AddModelError in Blazor Form #14524

ryanelian opened this issue Sep 27, 2019 · 2 comments
Labels
area-blazor Includes: Blazor, Razor Components question

Comments

@ryanelian
Copy link

ryanelian commented Sep 27, 2019

Is your feature request related to a problem? Please describe.

I am trying to add custom validation error message AFTER the form has been submitted successfully for an existing field.

In Razor Pages I can do something like: ModelState.AddModelError("Form.Field", "Something happened!"); which allows the error message to be displayed when the page is returned. (Bonus Point doing it like this because it allows the field CSS to also be displayed as errored field)

I can't find a way to do this in Blazor.

Describe the solution you'd like

I need a way to do ModelState.AddModelError in Blazor. Is it possible to expose ModelState altogether in Blazor?

Additional context

We apparently cannot simply use custom validation annotation because the validation may be async. (aspnet/Mvc#7791 required for this behavior)


Related to issue, might qualify as separate issue:

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
    var service = (IExternalService) validationContext
                         .GetService(typeof(IExternalService));
    // use service
}

In Blazor, GetService returns NULL!


EDIT 2: Solved the issue, although with a different approach:

https://github.com/ryanelian/FluentValidation.Blazor

This library allows writing validators registered to the .NET Core DI, which easily allow custom logic (even async) to be developed easily!

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Sep 27, 2019
@RemiBou
Copy link
Contributor

RemiBou commented Sep 30, 2019

I implemented custom error message in this blog post https://remibou.github.io/Using-the-Blazor-form-validation/ (search for "Display validation error from the server").

@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @ryanelian .
Glad that you've managed to find a solution for this.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components question
Projects
None yet
Development

No branches or pull requests

3 participants