Skip to content

Commit 3363dea

Browse files
authored
Merge pull request #5 from Liero/feature/v1
Feature/v1
2 parents 7d575b7 + 92a6116 commit 3363dea

22 files changed

Lines changed: 228 additions & 239 deletions

.github/workflows/ci-main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
types: [ published ]
88

99
env:
10-
NETCORE_VERSION: '5.0.x'
10+
NETCORE_VERSION: '6.0.x'
1111
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
1212
DOTNET_CLI_TELEMETRY_OPTOUT: true
1313
PROJECT_NAME: FluentValidation

.github/workflows/ci-pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66

77
env:
88
PROJECT_NAME: vNext.BlazorComponents.FluentValidation
9-
NETCORE_VERSION: '5.0.x'
9+
NETCORE_VERSION: '6.0.x'
1010

1111
jobs:
1212
build:

Demo/Demo.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
6-
6+
77
<ItemGroup>
8+
89
<ProjectReference Include="..\WebAssemblyDemo\WebAssemblyDemo.csproj" />
910
</ItemGroup>
1011

Demo/Program.cs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1+
using Microsoft.AspNetCore.Builder;
12
using Microsoft.AspNetCore.Hosting;
2-
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
34
using Microsoft.Extensions.Hosting;
4-
using Microsoft.Extensions.Logging;
5-
using System;
6-
using System.Collections.Generic;
7-
using System.Linq;
8-
using System.Threading.Tasks;
95

10-
namespace Demo
6+
7+
var builder = WebApplication.CreateBuilder(args);
8+
9+
// Add services to the container.
10+
builder.Services.AddRazorPages();
11+
builder.Services.AddServerSideBlazor();
12+
var app = builder.Build();
13+
14+
// Configure the HTTP request pipeline.
15+
if (!app.Environment.IsDevelopment())
1116
{
12-
public class Program
13-
{
14-
public static void Main(string[] args)
15-
{
16-
CreateHostBuilder(args).Build().Run();
17-
}
18-
19-
public static IHostBuilder CreateHostBuilder(string[] args) =>
20-
Host.CreateDefaultBuilder(args)
21-
.ConfigureWebHostDefaults(webBuilder =>
22-
{
23-
webBuilder.UseStartup<Startup>();
24-
});
25-
}
17+
app.UseExceptionHandler("/Error");
18+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
19+
app.UseHsts();
2620
}
21+
22+
app.UseHttpsRedirection();
23+
24+
app.UseStaticFiles();
25+
26+
app.UseRouting();
27+
app.MapRazorPages();
28+
app.MapControllers();
29+
app.MapBlazorHub();
30+
app.MapFallbackToPage("/_Host");
31+
32+
app.Run();

Demo/Startup.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

README.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# FluentValidation
2-
A library for using FluentValidation with Blazor
2+
A library for using FluentValidation with Blazor that supports async validation, severity levels and more.
33

44
![Build & Test Main](https://github.com/Liero/vNext.BlazorComponents.FluentValidation/workflows/Build%20&%20Test%20Main/badge.svg)
55

@@ -61,32 +61,29 @@ You can then use it as follows within a `EditForm` component.
6161
```
6262

6363
## Discovering Validators
64-
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.
64+
The component locates validators using `IValidatorFactory` optional service.
65+
The `DefaultValidatorFactory` implementation check for validators registered with DI first.
6566

66-
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.
67-
68-
```razor
69-
<FluentValidationValidator DisableAssemblyScanning="@true" />
70-
```
71-
**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.
67+
If it finds multiple validators, validators withing the same assembly and namespace are takes precedence.
68+
If it can't find any, it will then try scanning the applications assemblies
7269

70+
You can override default behaviour on by registering `IValidatorFactory` service:
7371

74-
For advanced scenarios, you can customize how the validators are discovered
72+
```csharp
73+
services.AddSingleton<IValidatorFactory>(new DefaultValidatorFactory { DisableAssemblyScanning = false })
74+
`
7575

76+
or per FluentValidationValidator component
7677
```razor
77-
<FluentValidationValidator ValidatorFactory="CreateValidator" />
78-
```
79-
```csharp
80-
IValidator CreateValidator(ValidatorFactoryContext ctx)
81-
{
82-
if (ctx.Model == Person.Address)
83-
{
84-
return new AddressValidator();
85-
}
86-
return (IValidator)ctx.ServiceProvider.GetService(ctx.ValidatorType);
87-
}
78+
<FluentValidationValidator ValidatorFactory="customValidatorFactory" />
79+
80+
@code {
81+
IValidatorFactory customValidatorFactory = new MyCustomValidatorFactory();
82+
}
8883
```
8984

85+
See [DefaultValidatorFactory.cs](vNext.BlazorComponents.FluentValidation\DefaultValidatorFactory.cs) for more info.
86+
9087
## Validating Complex Models
9188

9289
```csharp

WebAssemblyDemo/Models/Address.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using FluentValidation;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
1+
#nullable disable
2+
3+
using FluentValidation;
64

75
namespace WebAssemblyDemo.Models
86
{

WebAssemblyDemo/Models/EmailModel.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using FluentValidation;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
1+
#nullable disable
2+
3+
using FluentValidation;
64

75
namespace WebAssemblyDemo.Models
86
{

WebAssemblyDemo/Models/Person.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
using FluentValidation;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
62

73
namespace WebAssemblyDemo.Models
84
{
95
public class Person
106
{
11-
public string FirstName { get; set; }
12-
public string LastName { get; set; }
7+
public string? FirstName { get; set; }
8+
public string? LastName { get; set; }
139
public int? Age { get; set; }
1410
public Address Address { get; set; } = new Address();
1511
}

WebAssemblyDemo/Pages/Async.razor

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,25 @@
66
<hr class="mb-5" />
77

88
<EditForm Model="@Model" OnSubmit="@SubmitForm">
9-
<FluentValidationValidator />
9+
<FluentValidationValidator @ref="_validator" />
1010
<ValidationSummary />
1111

12-
<div class="form-group">
13-
<label>EmailAddress: </label>
12+
<div class="mb-3">
13+
<labe class="form-label">EmailAddress: </labe>
1414
<InputText @bind-Value="@Model.EmailAddress" class="form-control w-auto" />
1515
<ValidationMessage For="@(() => Model.EmailAddress )" />
1616
</div>
1717

1818
@if (!ValidationRunning)
1919
{
20-
<button class="btn btn-primary" type="submit">Save</button>
20+
<div class="d-flex gap-3">
21+
<button class="btn btn-primary" type="submit">Save</button>
22+
23+
@if (context.GetValidationMessages().Any())
24+
{
25+
<button class="btn btn-secondary" type="button" @onclick="Clear">Clear Errors</button>
26+
}
27+
</div>
2128
}
2229
else
2330
{
@@ -30,15 +37,19 @@
3037
<br />
3138

3239
@code {
33-
[Inject] IJSRuntime JS { get; set; }
40+
FluentValidationValidator? _validator;
41+
42+
[Inject] IJSRuntime JS { get; set; } = default!;
3443

3544
EmailModel Model { get; set; } = new EmailModel();
3645

3746
bool ValidationRunning { get; set; }
3847

48+
3949
async Task SubmitForm(EditContext editContext)
4050
{
4151
ValidationRunning = true;
52+
StateHasChanged();
4253
try
4354
{
4455
await editContext.ValidateAsync();
@@ -54,5 +65,10 @@
5465
}
5566
}
5667

68+
void Clear()
69+
{
70+
_validator?.ClearMessages();
71+
}
72+
5773

5874
}

0 commit comments

Comments
 (0)