Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Blazored/FluentValidation
A library for using FluentValidation with Blazor
https://github.com/Blazored/FluentValidation
blazor blazored csharp fluentvalidation hacktoberfest nuget
Last synced: 4 days ago
JSON representation
A library for using FluentValidation with Blazor
- Host: GitHub
- URL: https://github.com/Blazored/FluentValidation
- Owner: Blazored
- License: mit
- Created: 2019-06-13T21:45:47.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-07T13:12:53.000Z (about 1 month ago)
- Last Synced: 2024-10-29T17:29:25.035Z (14 days ago)
- Topics: blazor, blazored, csharp, fluentvalidation, hacktoberfest, nuget
- Language: C#
- Homepage: https://blazored.github.io/FluentValidation/
- Size: 9.63 MB
- Stars: 594
- Watchers: 13
- Forks: 85
- Open Issues: 49
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# FluentValidation
A library for using FluentValidation with Blazor[![Nuget version](https://img.shields.io/nuget/v/Blazored.FluentValidation.svg?logo=nuget)](https://www.nuget.org/packages/Blazored.FluentValidation/)
[![Nuget downloads](https://img.shields.io/nuget/dt/Blazored.FluentValidation?logo=nuget)](https://www.nuget.org/packages/Blazored.FluentValidation/)
![Build & Test Main](https://github.com/Blazored/FluentValidation/workflows/Build%20&%20Test%20Main/badge.svg)## Installing
You can install from Nuget using the following command:
`Install-Package Blazored.FluentValidation`
Or via the Visual Studio package manger.
## Basic Usage
Start by add the following using statement to your root `_Imports.razor`.```razor
@using Blazored.FluentValidation
```You can then use it as follows within a `EditForm` component.
```razor
Name:
Age:
Email Address:
Save
@code {
private Person _person = new();private void SubmitValidForm()
=> Console.WriteLine("Form Submitted Successfully!");
}
```## Finding Validators
By default, the component will check for validators registered with DI first. If it can't find, any it will then try scanning the applications assemblies to find validators using reflection.You can control this behaviour using the `DisableAssemblyScanning` parameter. If you only wish the component to get validators from DI, set the value to `true` and assembly scanning will be skipped.
```html
```
You can find examples of different configurations in the sample projects. The Blazor Server project is configured to load validators from DI only. The Blazor WebAssembly project is setup to load validators using reflection.
**Note:** When scanning assemblies the component will swallow any exceptions thrown by that process. This is to stop exceptions thrown by scanning third party dependencies crashing your app.
The validator must be publicly accessible and inherit directly from `AbstractValidator`.
## Async Validation
If you're using async validation, you can use the `ValidateAsync` method on the `FluentValidationValidator`.```razor
Name:
Age:
Email Address:
Save
@code {
private Person _person = new();
private FluentValidationValidator? _fluentValidationValidator;private async void SubmitFormAsync()
{
if (await _fluentValidationValidator!.ValidateAsync())
{
Console.WriteLine("Form Submitted Successfully!");
}
}
}
```## RuleSets
[RuleSets](https://docs.fluentvalidation.net/en/latest/rulesets.html) allow validation rules to be grouped and executed together while ignoring other rules. RulesSets are supported in two ways.The first is setting RuleSets via the `Options` parameter on the `FluentValidationValidator` component.
```razor
```
The second is when manually validating the model using the `Validate` or `ValidateAsync` methods.
```razor
@code {
private FluentValidationValidator? _fluentValidationValidator;private void PartialValidate()
=> _fluentValidationValidator?.Validate(options => options.IncludeRuleSets("Names"));
}
```## Access to full `ValidationFailure`
If you need details about the specifics of a validation result (e.g. its `Severity`), you can access the result of the
last validation by calling the `GetFailuresFromLastValidation` method on the `FluentValidationValidator` component.```razor
@code {
private FluentValidationValidator? _fluentValidationValidator;private string GetValidationClass()
{
var lastResult = _fluentValidationValidator?.GetFailuresFromLastValidation();
if (lastResult is null || !lastResult.Any())
{
return "valid";
}
if (lastResult.Any(failure => failure.Severity == Severity.Error))
{
return "invalid";
}
return "warning";
}
}
```