|
1 | 1 | # 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. |
3 | 3 |
|
4 | 4 |  |
5 | 5 |
|
@@ -61,32 +61,29 @@ You can then use it as follows within a `EditForm` component. |
61 | 61 | ``` |
62 | 62 |
|
63 | 63 | ## 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. |
65 | 66 |
|
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 |
72 | 69 |
|
| 70 | +You can override default behaviour on by registering `IValidatorFactory` service: |
73 | 71 |
|
74 | | -For advanced scenarios, you can customize how the validators are discovered |
| 72 | +```csharp |
| 73 | + services.AddSingleton<IValidatorFactory>(new DefaultValidatorFactory { DisableAssemblyScanning = false }) |
| 74 | +` |
75 | 75 |
|
| 76 | +or per FluentValidationValidator component |
76 | 77 | ```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 | + } |
88 | 83 | ``` |
89 | 84 |
|
| 85 | + See [DefaultValidatorFactory.cs](vNext.BlazorComponents.FluentValidation\DefaultValidatorFactory.cs) for more info. |
| 86 | + |
90 | 87 | ## Validating Complex Models |
91 | 88 |
|
92 | 89 | ```csharp |
|
0 commit comments