Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

Custom Data Annotation String Localizer / Extend asp-validation-for #175

Closed
cspaniard opened this issue Dec 16, 2015 · 5 comments
Closed

Comments

@cspaniard
Copy link

Hello,

I hope this is the right place, I followed samples and comments posted here and I was able to quickly write a proof of concept on a custom FileStringLocalizerFactory and FileStringLocalizer and register them in services at startup and DI working great.

So far so good... The problem I am facing is that tag-helpers like asp-validation-for use data annotations info on the model which kind of works if you use resource based localization. I would like to use my own.

I see how regular localization providers (controllers and views) try to be modular and plugable, and it works, but I cannot see a way to plug my own Data Annotation Localization provider.

I other words, how can I make tag-helpers like asp-validation-for take data from a custom annotation provider I would write (file based).

Here is the code in the model

    public class RegisterViewModel
    {
        [Required(ErrorMessageResourceType = typeof(My.Resources.RegisterViewModel),
            ErrorMessageResourceName = "EMAIL_REQUIRED")]
        [EmailAddress(ErrorMessageResourceType = typeof(My.Resources.RegisterViewModel),
            ErrorMessageResourceName = "EMAIL_INVALID")]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required(ErrorMessageResourceType = typeof(My.Resources.RegisterViewModel),
            ErrorMessageResourceName = "PASSWORD_REQUIRED")]
        [StringLength(maximumLength: 100, MinimumLength = 6,
            ErrorMessageResourceType = typeof(My.Resources.RegisterViewModel),
            ErrorMessageResourceName = "PASSWORD_MIN_LENGTH")]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm Password")]
        [Compare("Password",
            ErrorMessageResourceType = typeof(My.Resources.RegisterViewModel),
            ErrorMessageResourceName = "PASSWORD_NO_MATCH")]
        public string ConfirmPassword { get; set; }
    }

So when validation fails, I would like to pull the string from my provider/file instead of a resource file.

Either that or extend asp-validation-for so it can use another provider. That would work for me too.

TIA,
David

@hishamco
Copy link
Contributor

I think this a duplicate to an old issue #31

@cspaniard
Copy link
Author

I am sorry for dup... to tell you the truth trying to follow the thread is rather diffcult since info is somewhat broken down... ;)

Either way, I hope it can be resolved.

After reading issue #31, adding another parameter to the tag help passing the provider instance injected into the view, maybe is not a bad idea.

@hishamco
Copy link
Contributor

If the you have such suggestion in the tag helper, i think you should post it in the MVC repo

@cspaniard
Copy link
Author

I don't know enough about the insides of the architecture to make a call on what is the right way... I mentioned here a couple a couple of ideas since we are talking about localization. If indeed people think it is an ok thing to do, (which I dont know), then I can try to write something better in the MVC repo.

I do apologize if I dont do things completely right, I am very new to all this.
I appreciate your patience.

@DamianEdwards
Copy link
Member

This is being covered in the aspnet/Mvc#3518

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants