I have a MudForm and want to show a validation error, when no entries are present in the list of the model.
<MudForm><MudGrid Spacing="1" Class="pt-2"> @foreach (Product product in productList) {<MudItem xs="4"><MudNumericField Label="Amount" @bind-Value="product.Amount" Required="true" /></MudItem><MudItem xs="8"><MudTextField Label="Titel" @bind-Value="product.title" Required="true" /></MudItem> }</MudGrid></MudForm>In This example I want to display a validation error when productList is empty.In FluentValidations I have created a rule for it:
RuleFor(parent => parent.productList).NotEmpty();This works fine for MudBlazor Textfields and also using other rules. The above rule does not get triggered for the list though, probably since the error would not be shown anywhere.How can I add the validation message properly?
First try was just to add a Mud field to see if it triggers like this:
<MudTextField For="@(() => productList" T="IEnumerable<Product>" />This does indeed trigger the validator above and shows the error for this text field. But I do not want to add a text field just for a list validation to show.
Second try was using the blazor component ValidationMessage like this:
<ValidationMessage For="@(() => productList" />'This causes an exception, it does not work for MudBlazor but needs an EditForm instead. I would rather stay with MudBlazor since the component is already more complex than this example.
Error: System.InvalidOperationException:Microsoft.AspNetCore.Components.Forms.ValidationMessage
1[System.Collections.Generic.IEnumerable1[Product]]requires a cascading parameter of type EditContext.
How can I add the validation properly using MudBlazor?