Skip to content

Commit c787d87

Browse files
authored
Document validation models from different assemblies (#36246)
1 parent ff617d9 commit c787d87

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

aspnetcore/blazor/forms/validation.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,50 @@ The <xref:System.ComponentModel.DataAnnotations.CompareAttribute> doesn't work w
15721572

15731573
:::moniker range=">= aspnetcore-10.0"
15741574

1575+
## Use validation models from a different assembly
1576+
1577+
<!-- UPDATE 10.0 - API cross-links -->
1578+
1579+
For model validation defined in a different assembly, such as a library or the `.Client` project of a Blazor Web App:
1580+
1581+
* If the library is a plain class library (it isn't based on the `Microsoft.NET.Sdk.Web` or `Microsoft.NET.Sdk.Razor` SDKs), add a package reference to the library for the [`Microsoft.Extensions.Validation` NuGet package](https://www.nuget.org/packages/Microsoft.Extensions.Validation).
1582+
* Create a method in the library or `.Client` project that receives an <xref:Microsoft.Extensions.DependencyInjection.IServiceCollection> instance as an argument and calls `AddValidation` on it.
1583+
* In the app, call both the method and `AddValidation`.
1584+
1585+
The preceding approach results in validation of the types from both assemblies.
1586+
1587+
In the following example, the `AddValidationForTypesInClient` method is created for the `.Client` project of a Blazor Web App for validation using types defined in the `.Client` project.
1588+
1589+
`ServiceCollectionExtensions.cs` (in the `.Client` project):
1590+
1591+
```csharp
1592+
namespace BlazorSample.Client.Extensions;
1593+
1594+
public static class ServiceCollectionExtensions
1595+
{
1596+
public static IServiceCollection AddValidationForTypesInClient(
1597+
this IServiceCollection collection)
1598+
{
1599+
return collection.AddValidation();
1600+
}
1601+
}
1602+
```
1603+
1604+
In the server project's `Program` file, add the namespace and call the `.Client` project's service collection extension method (`AddValidationForTypesInClient`) and `AddValidation`:
1605+
1606+
```csharp
1607+
using BlazorSample.Client.Extensions;
1608+
1609+
...
1610+
1611+
builder.Services.AddValidationForTypesInClient();
1612+
builder.Services.AddValidation();
1613+
```
1614+
1615+
:::moniker-end
1616+
1617+
:::moniker range=">= aspnetcore-10.0"
1618+
15751619
## Nested objects and collection types
15761620

15771621
Blazor form validation includes support for validating properties of nested objects and collection items with the built-in <xref:Microsoft.AspNetCore.Components.Forms.DataAnnotationsValidator>.

aspnetcore/release-notes/aspnetcore-10/includes/blazor.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,13 @@ The <xref:Microsoft.AspNetCore.Components.Forms.DataAnnotationsValidator> compon
611611

612612
If one of the preceding steps produces a validation error, the remaining steps are skipped.
613613

614+
### Use validation models from a different assembly
615+
616+
You can validate forms with models defined in a different assembly, such as a library or the `.Client` project of a Blazor Web App, by creating a method in the library or `.Client` project that receives an <xref:Microsoft.Extensions.DependencyInjection.IServiceCollection> instance as an argument and calls `AddValidation` on it.
617+
* In the app, call both the method and `AddValidation`.
618+
619+
For more information and an example, see <xref:blazor/forms/validation#use-validation-models-from-a-different-assembly?view=aspnetcore-10.0>.
620+
614621
### Custom Blazor cache and `BlazorCacheBootResources` MSBuild property removed
615622

616623
Now that all Blazor client-side files are fingerprinted and cached by the browser, Blazor's custom caching mechanism and the `BlazorCacheBootResources` MSBuild property have been removed from the framework. If the client-side project's project file contains the MSBuild property, remove the property, as it no longer has any effect:

0 commit comments

Comments
 (0)