最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

blazor - Display validation message on a collection - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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!");
    }
}
发布评论

评论列表(0)

  1. 暂无评论