Blazor: Skip the initial validation for dynamically added form content
See original GitHub issueI’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:
- Created a year ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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.