I am conditionally showing some fields in a MudForm
and notice that the validation functions are not being triggered for these not initially shown components.
In the example below I have two MudTextField
that reside within a MudForm
- one being shown conditionally based on a checkbox.
The latter MudTextField
does not trigger the validation function unless it is initially shown. If it is initially shown it stops triggering the validation function after being hidden once.
What is the cause of this behaviour, and how can I properly attach/associate the
conditionally shown MudTextField
with the containing MudForm
?
@page "/page"
<MudForm
Model="@Model"
Validation="@(new Func<object, string, IEnumerable<string>>(ValidateCallback))"
ValidationDelay="0">
@* Validation function gets called for this component. *@
<MudTextField Label="Property" @bind-Value="@Model.Property" For="@(() => Model.Property)"></MudTextField>
@if (_visible)
{
// This component does not trigger the validation function.
<MudTextField Label="Property2" @bind-Value="@Model.Property2" For="@(() => Model.Property2)"></MudTextField>
}
</MudForm>
<MudCheckBox Label="Show/hide child-component" @bind-Checked="@_visible"></MudCheckBox>
@code {
class TestModel
{
public string Property { get; set; }
public string Property2 { get; set; }
}
private TestModel Model { get; } = new();
private bool _visible; // Even if component is initially shown, hiding it once makes the validation stop..
IEnumerable<string> ValidateCallback(object obj, string arg)
{
return new[] { $"{arg} validation failed!" };
}
}
It's working for me: Mudblazor snippet.
Make sure to use the latest Mudblazor version.