In Telerik for Blazor form fluentvalidator is not working child object/component

tb
tb
0 Points
0 Posts

In Telerik for Blazor form fluentvalidator is not working child object/component. See following:

@page "/"

<EditForm Model="@_person" OnValidSubmit="@SubmitValidForm">
    <FluentValidationValidator Validator="@_Validator" DisableAssemblyScanning="@true" />
    <ValidationSummary />
    <p>
        <label>First Name: </label>
        <InputText @bind-Value="@_person.FirstName" />
        <ValidationMessage For="@(() => _person.FirstName)" />
    </p>
    <p>
        <label>Last Name: </label>
        <InputText @bind-Value="@_person.LastName" />
        <ValidationMessage For="@(() => _person.LastName)" />
    </p>
    <hr />
    <p>
        <label>Age: </label>
        <InputNumber @bind-Value="@_person.Age" />
        <ValidationMessage For="@(() => _person.Age)" />
    </p>
    <p>
        <label>Email Address: </label>
        <InputText @bind-Value="@_person.EmailAddress" />
        <ValidationMessage For="@(() => _person.EmailAddress)" />
    </p>
    <p>
        <label>Address: Line 1: </label>
        <InputText @bind-Value="@_person.Address.Line1" />
        <ValidationMessage For="@(() => _person.Address.Line1)" />
    </p>
    <p>
        <label>Address: Line 2: </label>
        <InputText @bind-Value="@_person.Address.Line2" />
    </p>
    <p>
        <label>Address: Town: </label>
        <InputText @bind-Value="@_person.Address.Town" />
        <ValidationMessage For="@(() => _person.Address.Town)" />
    </p>

    <p>
        <label>Address: County: </label>
        <InputText @bind-Value="@_person.Address.County" />
        <ValidationMessage For="@(() => _person.Address.County)" />
    </p>
    <p>
        <label>Address: Postcode: </label>
        <InputText @bind-Value="@_person.Address.Postcode" />
        <ValidationMessage For="@(() => _person.Address.Postcode)" />
    </p>
    <button type="submit">Save</button>
</EditForm>

@code {
    private readonly Person _person = new();
    public PersonValidator _Validator { get; set; } = new PersonValidator();
}

Classes:

using FluentValidation;

namespace SharedModels
{
    public class Address
    {
        public string? Line1 { get; set; }
        public string? Line2 { get; set; }
        public string? Town { get; set; }
        public string? County { get; set; }
        public string? Postcode { get; set; }
    }

    public class AddressValidator : AbstractValidator<Address>
    {
        public AddressValidator()
        {
            RuleFor(p => p.Line1).NotEmpty().WithMessage("You must enter Line 1");
            RuleFor(p => p.Town).NotEmpty().WithMessage("You must enter a town");
            RuleFor(p => p.County).NotEmpty().WithMessage("You must enter a county");
            RuleFor(p => p.Postcode).NotEmpty().WithMessage("You must enter a postcode");
        }
    }
}
using FluentValidation;

namespace SharedModels
{
    public class Person
    {
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
        public int? Age { get; set; }
        public string? EmailAddress { get; set; }
        public Address Address { get; set; } = new();
    }

    public class PersonValidator : AbstractValidator<Person>
    {
        public PersonValidator()
        {
            RuleFor(p => p.Address).SetValidator(new AddressValidator());
        }
    }
}

 

Views: 93
Total Answered: 1
Total Marked As Answer: 1
Posted On: 09-Jan-2024 02:55

Share:   fb twitter linkedin
Answers
Smith
Smith
None
2568 Points
74 Posts
         

You need to pass validator context and some other changes:

Do following changes in razor component:

<EditForm Model="@_person" OnValidSubmit="@SubmitValidForm">
    <FluentValidationValidator @ref="_fluentValidationValidator DisableAssemblyScanning="@true" />
    <ValidationSummary />

In c# changes:

@code {
    private readonly Person _person = new();
    private FluentValidationValidator? _fluentValidationValidator;
}

And register it in program.cs

builder.Services.AddTransient<IValidator<Person>, PersonValidator>();
builder.Services.AddTransient<IValidator<Address>, AddressValidator>();

Note: Install latest version: 

Install-Package Blazored.FluentValidation
Posted On: 09-Jan-2024 05:32
Thanks. Works for me.
 - tb  09-Jan-2024 05:58
 Log In to Chat