Skip to content

Commit 39564cf

Browse files
committed
Fixed error when passed explicit Validator instance: System.InvalidOperationException: Cannot validate instances of type 'ModelSubType'. This validator can only validate instances of type 'ModelType'.
1 parent 0ad8f73 commit 39564cf

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

vNext.BlazorComponents.FluentValidation/FluentValidationValidator.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,15 @@ public virtual void ClearMessages()
8080
public virtual IValidator? ResolveValidator(FieldIdentifier fieldIdentifier = default)
8181
{
8282
if (EditContext == null) throw new InvalidOperationException("EditContext is null");
83-
if (Validator != null) return Validator;
84-
8583
object model = fieldIdentifier.Model ?? EditContext.Model;
86-
Type interfaceValidatorType = typeof(IValidator<>).MakeGenericType(model.GetType());
84+
Type modelType = model.GetType();
85+
86+
if (Validator != null && Validator.CanValidateInstancesOfType(modelType))
87+
{
88+
return Validator;
89+
}
90+
91+
Type interfaceValidatorType = typeof(IValidator<>).MakeGenericType(modelType);
8792
var ctx = new ValidatorFactoryContext(interfaceValidatorType, ServiceProvider, EditContext, model, fieldIdentifier);
8893
return ValidatorFactory.CreateValidator(ctx);
8994
}

vNext.BlazorComponents.FluentValidation/vNext.BlazorComponents.FluentValidation.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
<EmbedUntrackedSources>true</EmbedUntrackedSources>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
8-
<Version>1.1.2</Version>
9-
<PackageVersion>1.1.2</PackageVersion>
8+
<Version>1.1.3</Version>
9+
<PackageVersion>1.1.3</PackageVersion>
1010
<Authors>Liero; Daniel Turan</Authors>
1111
<Company>vNext Software Consulting</Company>
1212
<PackageId>vNext.BlazorComponents.FluentValidation</PackageId>

0 commit comments

Comments
 (0)