Skip to content

tenekon/Tenekon.FluentValidation.Extensions

Repository files navigation

Tenekon.FluentValidation.Extensions Starts License Activity Discord

🚧 This is a new project. You are very welcome to participate in this project, help shape the public API and share ideas. The project is currently in alpha state, so the public API may change significantly.

Table of Contents

Packages

Package Description
Tenekon.FluentValidation.Extensions.AspNetCore.Components
NuGet
Blazor integration for FluentValidation

Tenekon.FluentValidation.Extensions.AspNetCore.Components

An extension to integrate FluentValidation with ASP.NET Core Components (Blazor Components). This library enhances component-level validation in Blazor applications using FluentValidation.

📖 For usage examples, see the Validator Components Cookbook covering common and advanced scenarios.


Features

  • 🌞 Seamless integration with Blazor forms1
  • 🧩 Validate not only top-level anymore – just wrap each edit model part by a validator component2,4
  • 🧩 Nestable edit model validators – nested child validator components2 still hook into the validation of the main form3.
  • 🔌 Component-region validation — plug validators2 into forms or any part of a form or any nested region or subregion.3

1: Any form that provides a cascaded EditContext, even a plain CascadedValue Value="new EditContext(..)">..</CascadedValue> is sufficient.
2: Refers to the usage of validator components of this library.
3: Nested child edit model validators automatically receive the nearest EditContext, captured by the first validator component2 higher in the hierarchy (usually from a form1).
4: EditModelValidatorSubpath and EditModelScope have the capability to create an isolated EditContxt wired to the nearest EditContext3.

Quickstart

1. Install the NuGet package: NuGet

dotnet add package Tenekon.FluentValidation.Extensions.AspNetCore.Components

2. Define your validator using FluentValidation:

public class ModelValidator : AbstractValidator<MyModel>
{
    public ModelValidator()
    {
        RuleFor(x => x.Name).NotEmpty();
    }
}

Requires FluentValidation package to be installed.

3. Register the validator in your DI container:

builder.Services.AddValidatorsFromAssemblyContaining<ModelValidator>();

Requires FluentValidation.DependencyInjectionExtensions package to be installed.

4. Use the validator component(s) in your Blazor form:

@code {
    private Model _model = new Model {
        Name = default(string)
        Children = (List<SubModel>)[
            new SubModel {
                Name = default(string)
            }
        ]
    };
}
<EditForm Model="_model" OnValidSubmit="HandleValidSubmit">
    <EditModelValidatorRootpath ValidatorType="typeof(ModelValidator)" />
    <InputText @bind-Value="_model.Name" />
    <ValidationMessage For="() => _model.Name" />
    @foreach(var child in _model.Children) {
        <EditModelValidatorSubpath Model="child" ValidatorType="typeof(SubModelValidator)">
            <InputText @bind-Value="child.Name"/>
            <ValidationMessage For="() => child.Name"/>
            <!-- Also triggers whole form submission — just for demonstration purposes -->
            <button type="submit">Prematurely Submit</button>
        </EditModelValidatorSubpath>
    }
    <button type="submit">Submit</button>
</EditForm>

Documentation & Resources

Target Framework

  • .NET 8.0

Dependencies

  • FluentValidation 12.x
  • Microsoft.AspNetCore.Components.Forms 8.x
  • FastExpressionCompiler 5.x

Development

git clone https://github.com/tenekon/Tenekon.FluentValidation.Extensions.git
cd Tenekon.FluentValidation.Extensions
dotnet build

License

MIT License - see LICENSE for details.

Packages

No packages published

Languages