Code Monkey home page Code Monkey logo

Comments (13)

ryanelian avatar ryanelian commented on June 3, 2024 1

Thanks for the PR!

Sorry for the delay! I'm fixing this now along with the other issue. Gonna run some tests to make sure they work properly before publishing.

from fluentvalidation.blazor.

ryanelian avatar ryanelian commented on June 3, 2024

Sure, I don't mind adding support for FluentValidation 9.

(It will be a breaking changes though. Ideally it ships with .NET 5 but I don't mind doing it sooner)

I'm curious though, what makes you must use FluentValidation 9?

from fluentvalidation.blazor.

HansKlokkenspel avatar HansKlokkenspel commented on June 3, 2024

At the moment it's not necessarily a must, more that I just bumped into it when I installed the FluidValidation pack into another project which my blazor WebAssembly project had as dependency. Took a bit to figure out what was wrong exactly, just wanted it to bring it to your attention before other people came across it as well 👍

from fluentvalidation.blazor.

timbze avatar timbze commented on June 3, 2024

Just ran into this issue myself. I just updated cause it was available, and then I got the same error as above.

Rolling back FluentValidation.AspNetCore to 8.6.2 fixes the issue

from fluentvalidation.blazor.

DaneVinson avatar DaneVinson commented on June 3, 2024

This one bit me too. I was working on a POC and just installed the latest FluentValidation package (9.0.1) not knowing that v9 was just released. I kept getting the same error as Hans and was beating my head against the wall trying to figure out what was going on. Reverted to 8.6.2 and everything is grand now. Thank you @HansKlokkenspel for info.

from fluentvalidation.blazor.

sfmskywalker avatar sfmskywalker commented on June 3, 2024

I ended up on this issue with the same issue 😅 . I'm happy to clone & reference the source directly, but as @ryanelian mentioned, there are some breaking changes.

Specifically, this method doesn't compile anymore:

private ValidationResult TryValidateModel(EditContext editContext)
{
    try
    {
        return Validator.Validate(editContext.Model); // <-- Validator.Validate now accepts an IValidationContext, but editContext.Model is of type object
    }
   ...
}

I'm gonna try and cast it to IValidationContext and see what gives.

from fluentvalidation.blazor.

sfmskywalker avatar sfmskywalker commented on June 3, 2024

Yeah that didn't work 😁

from fluentvalidation.blazor.

sfmskywalker avatar sfmskywalker commented on June 3, 2024

Ah, but this does work: return Validator.Validate(new ValidationContext<object>(editContext.Model));

I'm not sure if there are any potential issues with doing ValidationContext<object>, but I'm gonna try and use reflection to instantiate a generically typed instance (so that the T in ValidationContext<T> is actually the type of the model).

from fluentvalidation.blazor.

sfmskywalker avatar sfmskywalker commented on June 3, 2024

I came up with this method to instantiate a ValidationContext<T>:

private IValidationContext CreateValidationContext(object model, PropertyChain propertyChain = default, IValidatorSelector validatorSelector = default)
{
    var contextType = typeof(ValidationContext<>);
    var modelType = model.GetType();
    var closedContextType = contextType.MakeGenericType(modelType);

    return (IValidationContext)Activator.CreateInstance(
        closedContextType, 
        model,
        propertyChain ?? new PropertyChain(), 
        validatorSelector ?? ValidatorOptions.Global.ValidatorSelectors.DefaultValidatorSelectorFactory());
}

But it fails in the browser with:

Cannot validate instances of type 'RuntimeType'. This validator can only validate instances of type 'MyModel'

I'll go with my former solution instead. I'll also send a PR with updated package references and code changes to accommodate the breaking changes of FluentValidations 9.

from fluentvalidation.blazor.

sfmskywalker avatar sfmskywalker commented on June 3, 2024

Ahhh wait, I made a mistake. It DOES work 😅 I was passing in the model TYPE, not the model itself 🤦😁

from fluentvalidation.blazor.

sfmskywalker avatar sfmskywalker commented on June 3, 2024

I created a PR here.

from fluentvalidation.blazor.

ryanelian avatar ryanelian commented on June 3, 2024

https://www.nuget.org/packages/Accelist.FluentValidation.Blazor/3.0.0

Uploaded package to nuget, wait for it to be indexed.

This version passes basic visual tests on both server-side and client-side Blazor, but as usual with breaking changes please test your codes for unexpected bugs.

from fluentvalidation.blazor.

sfmskywalker avatar sfmskywalker commented on June 3, 2024

Great stuff. Thanks for this release @ryanelian !

from fluentvalidation.blazor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.