Skip to content

Commit

Permalink
Merge pull request #5 from Liero/feature/v1
Browse files Browse the repository at this point in the history
Feature/v1
  • Loading branch information
Liero authored Mar 16, 2022
2 parents 7d575b7 + 92a6116 commit 3363dea
Show file tree
Hide file tree
Showing 22 changed files with 228 additions and 239 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
types: [ published ]

env:
NETCORE_VERSION: '5.0.x'
NETCORE_VERSION: '6.0.x'
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_CLI_TELEMETRY_OPTOUT: true
PROJECT_NAME: FluentValidation
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

env:
PROJECT_NAME: vNext.BlazorComponents.FluentValidation
NETCORE_VERSION: '5.0.x'
NETCORE_VERSION: '6.0.x'

jobs:
build:
Expand Down
7 changes: 4 additions & 3 deletions Demo/Demo.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>

<ProjectReference Include="..\WebAssemblyDemo\WebAssemblyDemo.csproj" />
</ItemGroup>

Expand Down
48 changes: 27 additions & 21 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Demo

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();
app.MapRazorPages();
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();
50 changes: 0 additions & 50 deletions Demo/Startup.cs

This file was deleted.

37 changes: 17 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# FluentValidation
A library for using FluentValidation with Blazor
A library for using FluentValidation with Blazor that supports async validation, severity levels and more.

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

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

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

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.

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

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

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

or per FluentValidationValidator component
```razor
<FluentValidationValidator ValidatorFactory="CreateValidator" />
```
```csharp
IValidator CreateValidator(ValidatorFactoryContext ctx)
{
if (ctx.Model == Person.Address)
{
return new AddressValidator();
}
return (IValidator)ctx.ServiceProvider.GetService(ctx.ValidatorType);
}
<FluentValidationValidator ValidatorFactory="customValidatorFactory" />

@code {
IValidatorFactory customValidatorFactory = new MyCustomValidatorFactory();
}
```

See [DefaultValidatorFactory.cs](vNext.BlazorComponents.FluentValidation\DefaultValidatorFactory.cs) for more info.

## Validating Complex Models

```csharp
Expand Down
8 changes: 3 additions & 5 deletions WebAssemblyDemo/Models/Address.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
#nullable disable

using FluentValidation;

namespace WebAssemblyDemo.Models
{
Expand Down
8 changes: 3 additions & 5 deletions WebAssemblyDemo/Models/EmailModel.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
#nullable disable

using FluentValidation;

namespace WebAssemblyDemo.Models
{
Expand Down
8 changes: 2 additions & 6 deletions WebAssemblyDemo/Models/Person.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebAssemblyDemo.Models
{
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public int? Age { get; set; }
public Address Address { get; set; } = new Address();
}
Expand Down
26 changes: 21 additions & 5 deletions WebAssemblyDemo/Pages/Async.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
<hr class="mb-5" />

<EditForm Model="@Model" OnSubmit="@SubmitForm">
<FluentValidationValidator />
<FluentValidationValidator @ref="_validator" />
<ValidationSummary />

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

@if (!ValidationRunning)
{
<button class="btn btn-primary" type="submit">Save</button>
<div class="d-flex gap-3">
<button class="btn btn-primary" type="submit">Save</button>

@if (context.GetValidationMessages().Any())
{
<button class="btn btn-secondary" type="button" @onclick="Clear">Clear Errors</button>
}
</div>
}
else
{
Expand All @@ -30,15 +37,19 @@
<br />

@code {
[Inject] IJSRuntime JS { get; set; }
FluentValidationValidator? _validator;

[Inject] IJSRuntime JS { get; set; } = default!;

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

bool ValidationRunning { get; set; }


async Task SubmitForm(EditContext editContext)
{
ValidationRunning = true;
StateHasChanged();
try
{
await editContext.ValidateAsync();
Expand All @@ -54,5 +65,10 @@
}
}

void Clear()
{
_validator?.ClearMessages();
}


}
2 changes: 1 addition & 1 deletion WebAssemblyDemo/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<br />

@code {
[Inject] IJSRuntime JS { get; set; }
[Inject] IJSRuntime JS { get; set; } = default!;

Person Person { get; set; } = new Person();

Expand Down
29 changes: 8 additions & 21 deletions WebAssemblyDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@

using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using WebAssemblyDemo;

namespace WebAssemblyDemo
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();
}
}
}
await builder.Build().RunAsync();
3 changes: 2 additions & 1 deletion WebAssemblyDemo/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

<div class="main">
<div class="top-row px-4">
<a href="https://github.com/Liero/vNext.BlazorComponents.FluentValidation" target="_blank" class="ml-md-auto">About</a>
FluentValidation
<a href="https://github.com/Liero/vNext.BlazorComponents.FluentValidation" target="_blank" class="ms-auto">About</a>
</div>

<div class="content px-4">
Expand Down
1 change: 0 additions & 1 deletion WebAssemblyDemo/Shared/MainLayout.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
.top-row {
background-color: #f7f7f7;
border-bottom: 1px solid #d6d5d5;
justify-content: flex-end;
height: 3.5rem;
display: flex;
align-items: center;
Expand Down
14 changes: 8 additions & 6 deletions WebAssemblyDemo/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<div class="top-row pl-4 navbar navbar-dark">
<a class="navbar-brand" href="">BlazorComponents FluentValidation</a>
<button class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
<div class="container-fluid">
<a class="navbar-brand" href="">vNext BlazorComponents</a>
<button class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</div>

<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
Expand All @@ -14,14 +16,14 @@
<NavLink class="nav-link" href="/async">
<span class="oi oi-clock" aria-hidden="true"></span> Async Validation
</NavLink>
</li>
</li>
</ul>
</div>

@code {
private bool collapseNavMenu = true;

private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;
private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null;

private void ToggleNavMenu()
{
Expand Down
Loading

0 comments on commit 3363dea

Please sign in to comment.