Files
Radzen.FluentValidation/README.md
2026-02-22 10:30:42 -06:00

86 lines
2.9 KiB
Markdown

# 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):
```cs
// Register your validators
services.AddTransient<IValidator<MyModel>, MyModelValidator>();
```
## Usage
Example model and validator:
```cs
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:
```razor
@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 `Component` parameter should match how you identify/find the Radzen field within the form (the component name used by your IRadzenForm implementation).
- Use `AsyncMode=true` to run validators via `ValidateAsync`.
- You can set `Validator` explicitly or let the component resolve an `IValidator<TModel>` from DI.
- `Selector` can 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](LICENSE) file for details.