question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor: Skip the initial validation for dynamically added form content

See original GitHub issue

I’ve been trying to skip the initial validation for dynamically added content but to no avail.

The problem

I have a form model object with a list of children objects. Whenever I add another child in the list during runtime, the child gets validatated even though the child’s fields were not even touched by the user. I would like for the newly added child to not be subject to validation right after creation. In other words, I want to skip the initial validation for such children.

Reproducing the problem (simplified example)

Index.razor html

<EditForm EditContext="EditContext" OnValidSubmit="@HandleValidSubmit">
        <ObjectGraphDataAnnotationsValidator />

        <div>
            <section>
                <label>
                    Name
                    <InputText @bind-Value="Model.Name" />
                    <ValidationMessage For="() => Model.Name" />
                </label>
            </section>

            @foreach (var item in Model.Items)
            {
                <section>
                    <label>
                        Name
                        <InputText @bind-Value="item.Name" />
                        <ValidationMessage For="() => item.Name" />
                    </label>
                </section>
            }
        </div>

        <section>
            <button @onclick="AddItem">Add Item</button>
            <button type="submit" disabled="@(!IsFormValid)">Create</button>
        </section>
</EditForm>

Index.razor code


...

protected override void OnInitialized()
{
    EditContext = new(Model);
    EditContext.OnFieldChanged += HandleFieldChanged;
    EditContext.OnValidationRequested += HandleValidationRequested;
    MessageStore = new(EditContext);
}

...

private void AddItem()
{
    Model.Items.Add(new ItemModel());
}

Any ideas?

More info

  • framework: .NET 6
  • project type: .NET Maui Blazor

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
IonutArhirecommented, Feb 17, 2023

Been away from github and my pet project for quite some time but today I had a look once more.

Turns out the described behaviour was due to my own code where I overwrote a validation lifecycle event.

Sorry for the mistake. This issue can be closed.

0reactions
msftbot[bot]commented, Feb 13, 2023

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set dynamic validations for Form fields
I am working on flow, where based on users selection in dropdown, I want to update the validations of TextBox field.
Read more >
How to validate a model in Blazor server app without ...
When the component is loaded, it loads its own model. When an input is modified, I need to check ChildModel and add it...
Read more >
ASP.NET Core Blazor forms and input components
Learn how to use forms with field validation and built-in input components in Blazor.
Read more >
Blazor WebAssembly Dynamic Form Validation
Using this Data Annotation attribute we can dynamically add or remove validation on a specific field in a form.
Read more >
Model validation in ASP.NET Core MVC and Razor Pages
Therefore, validation doesn't work automatically on dynamically generated forms.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found