diff --git a/Directory.Packages.support.props b/Directory.Packages.support.props
index a90352c34..6e8500d5c 100644
--- a/Directory.Packages.support.props
+++ b/Directory.Packages.support.props
@@ -1,4 +1,4 @@
-
+
@@ -8,5 +8,6 @@
+
diff --git a/sample/Sample.Restful.Client/Sample.Restful.Client.csproj b/sample/Sample.Restful.Client/Sample.Restful.Client.csproj
index fedba3b4f..18b47c8ed 100644
--- a/sample/Sample.Restful.Client/Sample.Restful.Client.csproj
+++ b/sample/Sample.Restful.Client/Sample.Restful.Client.csproj
@@ -11,6 +11,10 @@
/>
+
public class FluentValidator : ComponentBase
{
- private static readonly char[] separators = { '.', '[' };
+ private static readonly char[] _separators = { '.', '[' };
private static FieldIdentifier ToFieldIdentifier(EditContext editContext, string propertyPath)
{
@@ -27,7 +27,7 @@ private static FieldIdentifier ToFieldIdentifier(EditContext editContext, string
while (true)
{
- var nextTokenEnd = propertyPath.IndexOfAny(separators);
+ var nextTokenEnd = propertyPath.IndexOfAny(_separators);
if (nextTokenEnd < 0)
{
return new FieldIdentifier(obj, propertyPath);
@@ -69,87 +69,83 @@ private static FieldIdentifier ToFieldIdentifier(EditContext editContext, string
}
}
- ///
- /// The validator to validate against
- ///
- [Parameter]
- public IValidator Validator { get; set; } = null!;
-
- [Inject] private IValidatorFactory ValidatorFactory { get; set; } = null!;
-
- [CascadingParameter] private EditContext CurrentEditContext { get; set; } = null!;
-
- ///
- protected override void OnInitialized()
- {
- if (CurrentEditContext == null)
- {
- throw new InvalidOperationException(
- $"{nameof(FluentValidator)} requires a cascading " +
- $"parameter of type {nameof(EditContext)}. For example, you can use {nameof(FluentValidator)} " +
- $"inside an {nameof(EditForm)}."
- );
- }
-
- AddFluentValidation(Validator);
- }
-
- private void AddFluentValidation(IValidator validator)
+ private static void AddFluentValidation(IValidator? validator, EditContext editContext, IServiceProvider services)
{
- var messages = new ValidationMessageStore(CurrentEditContext);
+ var messages = new ValidationMessageStore(editContext);
- CurrentEditContext.OnValidationRequested +=
- (_, _) => ValidateModel(messages, validator);
+ editContext.OnValidationRequested +=
+ (_, _) => ValidateModel(messages, editContext, validator ?? services.GetValidator(editContext.Model.GetType()));
- CurrentEditContext.OnFieldChanged +=
- (_, eventArgs) => ValidateField(messages, eventArgs.FieldIdentifier, validator);
+ editContext.OnFieldChanged +=
+ (_, eventArgs) => ValidateField(messages, editContext, eventArgs.FieldIdentifier, validator ?? services.GetValidator(editContext.Model.GetType()));
}
- private async void ValidateModel(ValidationMessageStore messages, IValidator? validator = null)
+ private static async void ValidateModel(
+ ValidationMessageStore messages,
+ EditContext editContext,
+ IValidator? validator = null
+ )
{
- validator ??= GetValidatorForModel(CurrentEditContext.Model);
-
if (validator != null)
{
- var context = new ValidationContext