FluentValidation of a Complex Model not working after update to .net 7

1 Answer 932 Views
Form ValidationMessage
Nicolas
Top achievements
Rank 1
Iron
Nicolas asked on 14 Mar 2023, 02:00 PM

Hi

 

After our update to .Net 7, FluentValidation does not work with complex models anymore.

We're using FluentValidation 11.5.1 (latest)

Blazored.FluentValidation 2.1.0 (latest)

I get the error message: Cannot validate instance of type "The Composed Class Type ". Can only validate Instance of Type "Parent Class".

 

Has somebody a solution for this issue?

It worked fine in the previous version.

 

Thank you.

 

Nicolas
Top achievements
Rank 1
Iron
commented on 15 Mar 2023, 08:42 AM

So we found a Solution:

At least for Blazor Serverside. The example of Telerik is not working and throws an Exception.

We found the solution in the example section of the official Blazored.FluentValidation Repo:

https://github.com/Blazored/FluentValidation/blob/main/samples/BlazorServer/Pages/Index.razor

 

This did the Trick for us:

1) Adding ref

<FluentValidationValidator @ref="_fluentValidationValidator" DisableAssemblyScanning="@true" />

1) Using FluentValidationValidator class instead our own Validator

private FluentValidationValidator? _fluentValidationValidator;

 

3) As well as dependency injection, which actually makes code cleaner.:

services.AddTransient<IValidator<Model>, Validator>();

 

So for anyone new to using Blazored.FluentValidation -> It is a good idea to use the samples provided from git repo.

Well coding!

 

 

 

 

Viktor
Top achievements
Rank 1
commented on 08 Aug 2023, 10:01 AM

In this case there is another issue. What to do when there are two different validators for the same model (validate different sets of properties)? This solution wont work either

1 Answer, 1 is accepted

Sort by
0
Nicolas
Top achievements
Rank 1
Iron
answered on 16 Mar 2023, 10:00 AM
See solution above.
Tags
Form ValidationMessage
Asked by
Nicolas
Top achievements
Rank 1
Iron
Answers by
Nicolas
Top achievements
Rank 1
Iron
Share this question
or