I have student object with a child collection of Test. Have a validation on Tests collection defined in the StudentValidator to make sure at least one item is added to collection when saving a student.
In UI, loop through the collection in foreach and have individual textfields binded to Test object.
RuleFor(s => s.Tests).Must(s => s.Count() > 0).WithMessage("At least one Test info must be provided.");
Don't want to display in the ValidationMessage as remaining validations are displayed For() on the property on which validation message is defined.
Where/how do you display this validation message in the UI?
I have student object with a child collection of Test. Have a validation on Tests collection defined in the StudentValidator to make sure at least one item is added to collection when saving a student.
In UI, loop through the collection in foreach and have individual textfields binded to Test object.
RuleFor(s => s.Tests).Must(s => s.Count() > 0).WithMessage("At least one Test info must be provided.");
Don't want to display in the ValidationMessage as remaining validations are displayed For() on the property on which validation message is defined.
Where/how do you display this validation message in the UI?
Share Improve this question asked Mar 6 at 22:32 blue pinkblue pink 413 bronze badges1 Answer
Reset to default 0I don't see any conflict if you place it under the property like following sample. Anyway you could try display it under the submit button.
public class Test
{
public string Name { get; set; } = string.Empty;
}
public class Student
{
public string Name { get; set; } = string.Empty;
public List<Test> Tests { get; set; } = new();
}
public class StudentValidator : AbstractValidator<Student>
{
public StudentValidator()
{
RuleFor(s => s.Name)
.NotEmpty().WithMessage("Student name is required.");
RuleFor(s => s.Tests)
.Must(tests => tests != null && tests.Count > 3)
.WithMessage("At least one Test info must be provided.");
}
}
(used Blazored.FluentValidation
)
@using Blazored.FluentValidation
@using FluentValidation
@inject IValidator<Student> StudentValidator
<EditForm Model="@student" OnValidSubmit="SaveStudent">
<DataAnnotationsValidator />
<FluentValidationValidator Validator="StudentValidator" />
<h3>Student Form</h3>
<div>
<label>Student Name:</label>
<InputText @bind-Value="student.Name" />
<ValidationMessage For="@(() => student.Name)" />
</div>
<hr />
<h4>Tests</h4>
<ValidationMessage For="@(() => student.Tests)" />
<button type="button" @onclick="AddTest">Add Test</button>
<div>
@foreach (var test in student.Tests)
{
<div style="margin-bottom: 8px;">
<InputText @bind-Value="test.Name" placeholder="Enter Test Name" />
<button type="button" @onclick="() => RemoveTest(test)">Remove</button>
<ValidationMessage For="@(() => test.Name)" />
</div>
}
</div>
<hr />
<button type="submit">Save</button>
<ValidationSummary />
</EditForm>
@code {
private Student student = new Student();
private void AddTest()
{
student.Tests.Add(new Test());
}
private void RemoveTest(Test test)
{
student.Tests.Remove(test);
}
private void SaveStudent()
{
Console.WriteLine("Student saved successfully!");
}
}