diff --git a/aspnetcore/blazor/forms-validation.md b/aspnetcore/blazor/forms-validation.md index 8222b58862cf..76f6f4cdb6da 100644 --- a/aspnetcore/blazor/forms-validation.md +++ b/aspnetcore/blazor/forms-validation.md @@ -3924,8 +3924,83 @@ public class CustomValidator : ValidationAttribute } ``` -> [!NOTE] -> is `null`. Injecting services for validation in the `IsValid` method isn't supported. +Inject services into custom validation attributes through the . The following example demonstrates a salad chef form that validates user input with dependency injection (DI). + +The `SaladChef` class indicates the approved fruit ingredient list for a salad. + +`SaladChef.cs`: + +```csharp +public class SaladChef +{ + public string[] ThingsYouCanPutInASalad = { "Strawberries", "Pineapple", + "Honeydew", "Watermelon", "Grapes" }; +} +``` + +Register `SaladChef` in the app's DI container in `Program.cs`: + +```csharp +builder.Services.AddTransient(); +``` + +The `IsValid` method of the following `SaladChefValidatorAttribute` class obtains the `SaladChef` service from DI to check the user's input. + +`SaladChefValidatorAttribute.cs`: + +```csharp +using System.ComponentModel.DataAnnotations; + +public class SaladChefValidatorAttribute : ValidationAttribute +{ + protected override ValidationResult? IsValid(object? value, + ValidationContext validationContext) + { + var saladChef = validationContext.GetRequiredService(); + + if (saladChef.ThingsYouCanPutInASalad.Contains(value?.ToString())) + { + return ValidationResult.Success; + } + + return new ValidationResult("You should not put that in a salad!"); + } +} +``` + +The following `ValidationWithDI` component validates user input by applying the `SaladChefValidatorAttribute` (`[SaladChefValidator]`) to the salad ingredient string (`SaladIngredient`). + +`Pages/ValidationWithDI.razor`: + +```razor +@page "/validation-with-di" +@using System.ComponentModel.DataAnnotations +@using Microsoft.AspNetCore.Components.Forms + + + + +

+ Name something you can put in a salad: + +

+ + + +
    + @foreach (var message in context.GetValidationMessages()) + { +
  • @message
  • + } +
+ +
+ +@code { + [SaladChefValidator] + public string? SaladIngredient { get; set; } +} +``` ## Custom validation CSS class attributes