Skip to content

How to check if Model/ModelState is valid in Blazor? #12044

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

Closed
marcelovidigal opened this issue Jul 10, 2019 · 5 comments
Closed

How to check if Model/ModelState is valid in Blazor? #12044

marcelovidigal opened this issue Jul 10, 2019 · 5 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation task
Milestone

Comments

@marcelovidigal
Copy link

marcelovidigal commented Jul 10, 2019

Is there anything like TryValidadeModel in Blazor? How can I check in @code if the model passed to EditForm is valid or not?

@marcelovidigal marcelovidigal changed the title How to check if Model/ModelState is valid in blazor? How to check if Model/ModelState is valid in Blazor? Jul 10, 2019
@mkArtakMSFT mkArtakMSFT added area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation labels Jul 10, 2019
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @marcelovidigal.
You have to create the EditContext yourself to do this. We're going to use this issue to improve the docs to include such an example.

@mkArtakMSFT mkArtakMSFT modified the milestones: preivew9, 3.0.0 Jul 10, 2019
@enetstudio
Copy link

I guess this can help you solve your issue...

/// <summary>
        /// A callback that will be invoked when the form is submitted.
        ///
        /// If using this parameter, you are responsible for triggering any validation
        /// manually, e.g., by calling <see cref="EditContext.Validate"/>.
        /// </summary>
        [Parameter] public EventCallback<EditContext> OnSubmit { get; private set; }

Source: https://github.com/aspnet/AspNetCore/blob/993d943aec5c8bd590524d56b0c8cdec27032767/src/Components/Components/src/Forms/EditForm.cs

@mkArtakMSFT mkArtakMSFT modified the milestones: 3.0.0, 3.0.0-rc1 Aug 14, 2019
@danroth27 danroth27 modified the milestones: 3.0.0-rc1, 3.0.0 Aug 19, 2019
@mkArtakMSFT mkArtakMSFT modified the milestones: 3.0.0, 3.1.0-preview4 Sep 24, 2019
@mkArtakMSFT mkArtakMSFT modified the milestones: 3.1.0-preview4, 3.1.0-preview3 Oct 1, 2019
@mkArtakMSFT mkArtakMSFT modified the milestones: 3.1.0-preview3, 3.1.0 Nov 1, 2019
@pranavkm pranavkm removed this from the 3.1.0 milestone Dec 9, 2019
@mkArtakMSFT mkArtakMSFT added this to the 5.0.0-preview1 milestone Dec 10, 2019
@mkArtakMSFT
Copy link
Member

We've improved our Forms and validation docs quite a bit. This should be covered .there already: https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1
As pointed out in the docs you can now call Validate on the EditContext.

@glararan
Copy link

@mkArtakMSFT can you provide please guide how to remove field from validation? Like from MVC via ModelState.Remove("....."). I am using same class for database where I require those attributes to be required. I fill them after post. Or is there way to do action before validation? (I guess somehow via EditContext?)

@mkArtakMSFT
Copy link
Member

Hi @glararan.

It looks like you are posting on a closed issue!
We're very likely to lose track of your bug/feedback/question unless you:

  1. Open a new issue
  2. Explain very clearly what you need help with
  3. If you think you have found a bug, include detailed repro steps so that we can investigate the problem

@ghost ghost locked as resolved and limited conversation to collaborators Mar 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation task
Projects
None yet
Development

No branches or pull requests

7 participants