2.9 KiB
2.9 KiB
Radzen.FluentValidation
A small Blazor integration that connects FluentValidation with Radzen forms. It provides a Radzen-compatible validator component (RadzenFluentValidator) that attaches to a RadzenTemplateForm, uses FluentValidation validators (sync or async), and displays validation messages in Radzen style (inline or as tooltip popups).
Features
- Integrates FluentValidation with RadzenTemplateForm.
- Per-field validation and message display.
- Supports synchronous and asynchronous validation.
- Optional tooltip popup display with configurable position.
- Supports custom IValidator and IValidatorSelector injection.
Installation
- Add the project to your solution or reference the compiled assembly.
- Ensure you have Radzen.Blazor and FluentValidation referenced in your Blazor project.
Register FluentValidation validators in DI (example in Startup.cs / Program.cs):
// Register your validators
services.AddTransient<IValidator<MyModel>, MyModelValidator>();
Usage
Example model and validator:
public class MyModel
{
public string Name { get; set; }
}
public class MyModelValidator : AbstractValidator<MyModel>
{
public MyModelValidator()
{
RuleFor(x => x.Name).NotEmpty().WithMessage("Name is required.");
}
}
Example Blazor (Razor) usage with RadzenTemplateForm:
@using Radzen.Blazor
@using Radzen.FluentValidation
<RadzenTemplateForm TItem=MyModel Data=model Submit=OnSubmit>
<FluentValidator />
<ValidationSummary />
<RadzenStack Orientation=Orientation.Vertical Gap="8px">
@* Name field *@
<RadzenFormField Text=@Strings[nameof(AccountModel.Name)]>
<ChildContent>
<RadzenTextBox @bind-Value=@Item.Name Name="input-name" />
</ChildContent>
<Helper>
<RadzenFluentValidator Component="input-name" />
</Helper>
</RadzenFormField>
</RadzenStack>
</RadzenTemplateForm>
@code {
private MyModel model = new MyModel();
void OnSubmit()
{
// form submit logic
}
}
Notes:
- The
Componentparameter should match how you identify/find the Radzen field within the form (the component name used by your IRadzenForm implementation). - Use
AsyncMode=trueto run validators viaValidateAsync. - You can set
Validatorexplicitly or let the component resolve anIValidator<TModel>from DI. Selectorcan be provided to control which FluentValidation rules run.
Behavior
- The component attaches to the form's EditContext and uses a ValidationMessageStore to publish FluentValidation errors to Blazor's validation system.
- Shows combined messages for a field and updates when validation state changes.
Contributing
Contributions are welcome. Open issues or PRs with improvements or bug fixes.
License
This project is licensed under the MIT License - see the LICENSE file for details.