Giter VIP home page Giter VIP logo

Comments (7)

jefflaia avatar jefflaia commented on August 29, 2024

I've found a way to let it work.
in your Application_Start don't clear ModelValidatorProviders.provoders, and don't add LocalizedModelValidatorProvider to Providers collection, just set the ModelMetadataProviders.Current to LocalizedModelMetadataProvider.

The follow example code will be called by Application_Start() in Global.asax.cs

private void SetupLocalizationProviders()
{
//ModelValidatorProviders.Providers.Clear(); // Don't execute this for customized DataAnnotation work
ModelMetadataProviders.Current = new LocalizedModelMetadataProvider();
//ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider()); // Don't execute this for customized DataAnnotation work
}

from griffin.mvccontrib.

mpumcubido avatar mpumcubido commented on August 29, 2024

Hi,

I tried your fix but actually the localization is not working anymore. Just the default texts are selected, not the ones from the database.

Is there a real fix how to get the SQL Server Provider working with Client Validation?

from griffin.mvccontrib.

jgauffin avatar jgauffin commented on August 29, 2024

The code is available here: https://github.com/jgauffin/griffin.mvccontrib/blob/master/source/Griffin.MvcContrib/Localization/LocalizedModelValidatorProvider.cs

Feel free to try to provide a fix.

from griffin.mvccontrib.

jefflaia avatar jefflaia commented on August 29, 2024

Hi jgauffin,
I noticed in your code the class ValidationAttributeAdapterFactory constructor calls MapDefaultRules(), only init the built-in Model Client Validation Rules. can you provide a way to add more client validation rules for custom attributes?

from griffin.mvccontrib.

jefflaia avatar jefflaia commented on August 29, 2024

step 1: modify the class LocalizedModelValidatorProvider, add a property as following code:

    public ValidationAttributeAdapterFactory ClientValidationRuleFactory
    { 
        get 
        {
            return _adapterFactory;
        } 
    }

step 2: add helper class in your MVC project to extend the client validation rules, the following example code is for DataAnnotationsExtensions and MVC Foolproof Validation:

public static class ClientValidationRuleFactoryHelper
{
    public static void MapClientValidationRules(ValidationAttributeAdapterFactory factory)
    {
        MapExtensionClientValidationRules(factory);
        MapFoolProofClientValidationRules(factory);
    }

    private static void MapExtensionClientValidationRules(ValidationAttributeAdapterFactory factory)
    {
        AddDelegateRule<EmailAttribute>(factory, (attribute, errMsg) =>
        {
            return new[]
                {
                    new ModelClientValidationEmailRule(errMsg)
                };
        });

        AddDelegateRule<MaxAttribute>(factory, (attribute, errMsg) =>
        {
            var attr = (MaxAttribute)attribute;
            return new[]
                {
                    new ModelClientValidationMaxRule(errMsg, attr.Max)
                };
        });

        // ---- other attributes client validation rules

    }

    private static void MapFoolProofClientValidationRules(ValidationAttributeAdapterFactory factory)
    {
        AddDelegateRule<GreaterThanAttribute>(factory, (attribute, errMsg) =>
        {
            var attr = (ContingentValidationAttribute)attribute;

            var rule = new ModelClientValidationRule() { ErrorMessage = errMsg, ValidationType = attr.ClientTypeName.ToLower() };

            foreach (var pair in attr.ClientValidationParameters)
            {
                rule.ValidationParameters[pair.Key] = pair.Value;
            }

            return new[]
                {
                    rule
                };
        });

        // ---- other attributes client validation rules
    }


    private static void AddDelegateRule<T>(ValidationAttributeAdapterFactory factory,
        Func<ValidationAttribute, string, IEnumerable<ModelClientValidationRule>> func)
        where T : ValidationAttribute
    {
        factory.Map<T>(new DelegateValidationAttributeAdapterFactory(func));
    }
}

Step 3: in Global.asax.cs file:

    private void SetupLocalizationProviders()
    {
        var provider = ModelValidatorProviders.Providers.Where(p => p.GetType() == typeof(DataAnnotationsModelValidatorProvider)).FirstOrDefault();
        if (provider != null)
        {
            ModelValidatorProviders.Providers.Remove(provider);
        }
        ModelMetadataProviders.Current = new LocalizedModelMetadataProvider();
        var localProvider = new LocalizedModelValidatorProvider();
        ModelValidatorProviders.Providers.Add(localProvider);

        //Register Client Validation Rules
        ClientValidationRuleFactoryHelper.MapClientValidationRules(localProvider.ClientValidationRuleFactory);
    }

from griffin.mvccontrib.

jgauffin avatar jgauffin commented on August 29, 2024

thank you. :) I'll merge that code asap

from griffin.mvccontrib.

sarae avatar sarae commented on August 29, 2024

Hi,
The griffin.MvcContrib is a clean solution for localization :)
I have the same problem with foolproof and DataAnnotationExtensions.

When can I get the new version?

Thank you!

from griffin.mvccontrib.

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.