forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IServiceProvider to DataAnnotationsValidator (dotnet#39445)
- Loading branch information
1 parent
9b14ab1
commit 68ff4c0
Showing
10 changed files
with
110 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#nullable enable | ||
static Microsoft.AspNetCore.Components.Forms.EditContextDataAnnotationsExtensions.EnableDataAnnotationsValidation(this Microsoft.AspNetCore.Components.Forms.EditContext! editContext, System.IServiceProvider! serviceProvider) -> System.IDisposable! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Components/test/testassets/BasicTestApp/FormsTest/ValidationComponentDI.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@using System.ComponentModel.DataAnnotations | ||
@using Microsoft.AspNetCore.Components.Forms | ||
|
||
<EditForm Model="@this" autocomplete="off"> | ||
<DataAnnotationsValidator /> | ||
|
||
<p class="the-quiz"> | ||
Name something you can put in a salad: | ||
<input @bind="SaladIngredient" class="@context.FieldCssClass(() => SaladIngredient)" /> | ||
</p> | ||
|
||
<button type="submit">Submit</button> | ||
<ul class="validation-errors"> | ||
@foreach (var message in context.GetValidationMessages()) | ||
{ | ||
<li class="validation-message">@message</li> | ||
} | ||
</ul> | ||
|
||
</EditForm> | ||
|
||
@code { | ||
[SaladChefValidator] | ||
public string SaladIngredient { get; set; } | ||
|
||
public class SaladChefValidatorAttribute : ValidationAttribute | ||
{ | ||
protected override ValidationResult IsValid(object value, ValidationContext validationContext) | ||
{ | ||
var saladChef = validationContext.GetRequiredService<SaladChef>(); | ||
if (saladChef.ThingsYouCanPutInASalad.Contains(value.ToString())) | ||
{ | ||
return ValidationResult.Success; | ||
} | ||
return new ValidationResult("You should not put that in a salad!"); | ||
} | ||
} | ||
|
||
// Simple class to check if DI can be used in Validation attributes | ||
public class SaladChef | ||
{ | ||
public string[] ThingsYouCanPutInASalad = { "Strawberries", "Pineapple", "Honeydew", "Watermelon", "Grapes" }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters