diff --git a/samples/BlazorServer/Pages/Index.razor b/samples/BlazorServer/Pages/Index.razor index f227a9f..a72800f 100644 --- a/samples/BlazorServer/Pages/Index.razor +++ b/samples/BlazorServer/Pages/Index.razor @@ -5,7 +5,7 @@
- +

@@ -72,6 +72,10 @@ @code { private readonly Person _person = new(); private FluentValidationValidator? _fluentValidationValidator; + private readonly IDictionary _data = new Dictionary + { + { "MinAgeLimit", 18 } + }; private void SubmitValidForm() => Console.WriteLine("Form Submitted Successfully!"); diff --git a/samples/BlazorWebAssembly/Pages/Index.razor b/samples/BlazorWebAssembly/Pages/Index.razor index 400e083..d1b1494 100644 --- a/samples/BlazorWebAssembly/Pages/Index.razor +++ b/samples/BlazorWebAssembly/Pages/Index.razor @@ -5,7 +5,7 @@


- +

@@ -72,6 +72,10 @@ @code { private readonly Person _person = new(); private FluentValidationValidator? _fluentValidationValidator; + private readonly IDictionary _data = new Dictionary + { + { "MinAgeLimit", 18 } + }; private async Task SubmitFormAsync() { diff --git a/samples/Shared/SharedModels/Person.cs b/samples/Shared/SharedModels/Person.cs index 412ae40..6462dd6 100644 --- a/samples/Shared/SharedModels/Person.cs +++ b/samples/Shared/SharedModels/Person.cs @@ -28,6 +28,13 @@ public PersonValidator() RuleFor(p => p.Age) .NotNull().WithMessage("You must enter your age") + .Custom((age, context) => + { + if (context.RootContextData.TryGetValue("MinAgeLimit", out var ageValue) && ageValue is int minAge && age < minAge) + { + context.AddFailure($"Age must be greater than {minAge}"); + } + }) .GreaterThanOrEqualTo(0).WithMessage("Age must be greater than 0") .LessThan(150).WithMessage("Age cannot be greater than 150"); diff --git a/src/Blazored.FluentValidation/EditContextFluentValidationExtensions.cs b/src/Blazored.FluentValidation/EditContextFluentValidationExtensions.cs index c06c9f4..f90fa1e 100644 --- a/src/Blazored.FluentValidation/EditContextFluentValidationExtensions.cs +++ b/src/Blazored.FluentValidation/EditContextFluentValidationExtensions.cs @@ -23,7 +23,7 @@ public static void AddFluentValidation(this EditContext editContext, IServicePro async (sender, _) => await ValidateModel((EditContext)sender!, messages, serviceProvider, disableAssemblyScanning, fluentValidationValidator, validator); editContext.OnFieldChanged += - async (_, eventArgs) => await ValidateField(editContext, messages, eventArgs.FieldIdentifier, serviceProvider, disableAssemblyScanning, validator); + async (_, eventArgs) => await ValidateField(editContext, messages, eventArgs.FieldIdentifier, serviceProvider, disableAssemblyScanning, fluentValidationValidator, validator); } private static async Task ValidateModel(EditContext editContext, @@ -52,6 +52,8 @@ private static async Task ValidateModel(EditContext editContext, context = new ValidationContext(editContext.Model); } + FillRootContextData(context, fluentValidationValidator.ContextData); + var asyncValidationTask = validator.ValidateAsync(context); editContext.Properties[PendingAsyncValidation] = asyncValidationTask; var validationResults = await asyncValidationTask; @@ -72,11 +74,14 @@ private static async Task ValidateField(EditContext editContext, FieldIdentifier fieldIdentifier, IServiceProvider serviceProvider, bool disableAssemblyScanning, + FluentValidationValidator fluentValidationValidator, IValidator? validator = null) { var properties = new[] { fieldIdentifier.FieldName }; var context = new ValidationContext(fieldIdentifier.Model, new PropertyChain(), new MemberNameValidatorSelector(properties)); - + + FillRootContextData(context, fluentValidationValidator.ContextData); + validator ??= GetValidatorForModel(serviceProvider, fieldIdentifier.Model, disableAssemblyScanning); if (validator is not null) @@ -219,4 +224,15 @@ private static FieldIdentifier ToFieldIdentifier(in EditContext editContext, in } } } + + private static void FillRootContextData(ValidationContext context, IDictionary? data) + { + if (data is not null) + { + foreach (var item in data) + { + context.RootContextData[item.Key] = item.Value; + } + } + } } \ No newline at end of file diff --git a/src/Blazored.FluentValidation/FluentValidationsValidator.cs b/src/Blazored.FluentValidation/FluentValidationsValidator.cs index ae8e6f9..4448359 100644 --- a/src/Blazored.FluentValidation/FluentValidationsValidator.cs +++ b/src/Blazored.FluentValidation/FluentValidationsValidator.cs @@ -16,6 +16,7 @@ public class FluentValidationValidator : ComponentBase [Parameter] public IValidator? Validator { get; set; } [Parameter] public bool DisableAssemblyScanning { get; set; } [Parameter] public Action>? Options { get; set; } + [Parameter] public IDictionary? ContextData { get; set; } internal Action>? ValidateOptions { get; set; } public bool Validate(Action>? options = null)