Blazor and ValidationGroups

Kuler Master 406 Reputation points
2025-01-04T13:22:46.66+00:00

I am in ASP.NET since the first version of .NET Framework and I am used to some features that are missing now. Please notice that I've just recently moved to Blazor.

Currently, I am wondering why the ValidationGroups are not part of it. I have a kind of Wizard with 3 steps and I need to validate every step separately.

For example, firstly I gather the user name. In the second contact details and in the last step I ask them to upload some documents.

With the ValidationGroups it was super easy in the past, but now I am stuck considering to remove the steps and have a single form - this would be a kind of compromise.

Thank you

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | Blazor
Developer technologies | ASP.NET | Other
{count} votes

5 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
    2025-01-04T17:06:57.4233333+00:00

    Blazor is SPA with components, there is no postback. Each wizard page is its own component and has its own page model. On navigation it validates the page model.

    if converting an aspx page that had two groups, in Blazor you’d make a component for each group.


  2. AgaveJoe 30,206 Reputation points
    2025-01-05T15:02:31.1066667+00:00

    If so, how to I get all the info at the end (last component)? Meaning, I need to save user name, contact info and finally the documents.

    Typically data is stored in a database. The data is fetched using a unique key. The key can be passed to components as a route parameter.

    https://learn.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-9.0#route-parameters

    How to make them communicate between each other?

    There are many ways to pass data or persist state. Perhaps if you give us an example that illustrates the problem we can fill in the gaps or provide an alternative approach. Or perhaps reading the official documentation will help.

    https://learn.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-9.0#child-content-render-fragments

    https://learn.microsoft.com/en-us/aspnet/core/blazor/components/cascading-values-and-parameters?view=aspnetcore-9.0

    https://learn.microsoft.com/en-us/aspnet/core/blazor/components/cascading-values-and-parameters?view=aspnetcore-9.0#pass-data-across-a-component-hierarchy

    https://learn.microsoft.com/en-us/aspnet/core/blazor/state-management?view=aspnetcore-9.0&pivots=server


  3. Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
    2025-01-05T17:47:10.5566667+00:00

    Yes you can have multiple edit forms on a page. But with a wizard, only one wizard component would be rendered at a time.

    I would expect the page hosting the wizard would manage state and its persistence. The wizard components would just update this state.

    also as an app refresh/reset loses all in memory state data, you should spend some effort and persisting state for recovery, especially with a wizard.

    0 comments No comments

  4. Anonymous
    2025-01-06T03:42:31.7266667+00:00

    Hi @Kuler Master

    can I have multiple EditForms in a single razor compenent/page?

    Yes, you can use multiple edit forms in a single Razor component/page. It will validate each form separately and you can access the data of different forms in the OnValidSubmit method.

    Refer to the following sample code:

        @page "/multiple-forms"
        @using System.ComponentModel.DataAnnotations
    
        <h3>Form 1</h3>
        <EditForm Model="@form1Model" OnValidSubmit="HandleSubmitForm1">
            <DataAnnotationsValidator />
            <ValidationSummary />
    
            <div>
                <label>Field 1:</label>
                <InputText id="field1" @bind-Value="form1Model.Field1" />
                <ValidationMessage For="@(() => form1Model.Field1)" />
            </div>
            <button type="submit">Submit Form 1</button>
        </EditForm>
    
        <h3>Form 2</h3>
        <EditForm Model="@form2Model" OnValidSubmit="HandleSubmitForm2">
            <DataAnnotationsValidator />
            <ValidationSummary />
    
            <div>
                <label>Field 2:</label>
                <InputText id="field2" @bind-Value="form2Model.Field2" />
                <ValidationMessage For="@(() => form2Model.Field2)" />
            </div>
            <button type="submit">Submit Form 2</button>
        </EditForm>
    
        @code {
            private Form1Model form1Model = new();
            private Form2Model form2Model = new();
    
            private void HandleSubmitForm1()
            {
                // Process form 1 data
            }
    
            private void HandleSubmitForm2()
            {   // Access form1Model data here
                var field1Data = form1Model.Field1;
            
                // Process form 2 data along with form 1 data if needed 
            }
    
            public class Form1Model
            {
                [Required]
                public string Field1 { get; set; }
            }
    
            public class Form2Model
            {
                [Required]
                public string Field2 { get; set; }
            }
        }
    
    

    The result as below:

    After submitting form 1:

    User's image

    After submitting form 2:

    User's image

    And we can get the form1 data in the Form2's OnValidSubmit method:

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

  5. Jack Dang (WICLOUD CORPORATION) 1,020 Reputation points Microsoft External Staff
    2025-07-14T07:01:17.4333333+00:00

    Hello @Kuler Master ,

    Thanks for reaching out.

    Blazor does not include built-in support for ValidationGroups like ASP.NET Web Forms. This is a deliberate design choice, reflecting Blazor’s modern, component-based architecture and its reliance on EditForm and data annotations for validation.

    That said, you can achieve similar behavior using a few alternative strategies:

    • Separate EditForms: Divide your form into multiple EditForm components, each with its own model and validation context. This effectively isolates validation logic per section.
    • Custom Validation Logic: Use Blazor’s EditContext and ValidationMessageStore to manually control which fields are validated and when. This gives you fine-grained control over validation behavior.
    • Conditional Validation Attributes: Implement custom validation attributes that activate based on a group identifier or context. This allows selective validation depending on the current form state or user action.

    These approaches align with Blazor’s philosophy of clean separation of concerns and reusable components. While they may require more setup than Web Forms, they often result in more maintainable and scalable applications.

    Hope this helps.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.