🚧 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.
| Package Description | |
|---|---|
Tenekon.FluentValidation.Extensions.AspNetCore.Components |
Blazor integration for FluentValidation |
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.
- 🌞 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.
dotnet add package Tenekon.FluentValidation.Extensions.AspNetCore.Components2. Define your validator using FluentValidation:
public class ModelValidator : AbstractValidator<MyModel>
{
public ModelValidator()
{
RuleFor(x => x.Name).NotEmpty();
}
}Requires
FluentValidationpackage to be installed.
3. Register the validator in your DI container:
builder.Services.AddValidatorsFromAssemblyContaining<ModelValidator>();Requires
FluentValidation.DependencyInjectionExtensionspackage 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>- 📖 Cookbook: Validator Components Cookbook — Examples & use cases for all common and advanced scenarios.
- 🧱 Architecture Validator Components Architecture — Architectural approach of the components, and their integration with Blazor's EditContext.
- 🔬 Flow Logic: Validator Components Flow Logic — Visual guide to
how edit model validators interact with each other and Blazor's
EditContext.
.NET 8.0
FluentValidation12.xMicrosoft.AspNetCore.Components.Forms8.xFastExpressionCompiler5.x
git clone https://github.com/tenekon/Tenekon.FluentValidation.Extensions.git
cd Tenekon.FluentValidation.Extensions
dotnet buildMIT License - see LICENSE for details.