Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localize Compare attribute

At the bottom of this question is a model containing three properties: OldPassword, NewPassword and ConfirmPassword. These display in a Change Password form. I've localized all of the error messages, except one: I cannot fully localize the error message when the Compare attribute fails. As you'll see, I'm reading the error message from a string named FieldMismatch in my resource file. Here's that string in the Spanish resource file:

El campo {0} y {1} campo no coinciden.

The {0} part is correctly getting replaced with the translation for OldPassword, but i don't know how to localize the pointer to NewPassword.

So to recap, I'm looking a way to replace [Compare("NewPassword", ... with [Compare(Resources.Culture.Account.Account.NewPassword, ...

Anyone have any thoughts on the best way to accomplish this?

public class LocalPasswordModel
{
    [Required(ErrorMessageResourceName = "FieldIsRequired", ErrorMessageResourceType = typeof(Resources.Culture.Home.Global))]
    [DataType(DataType.Password)]
    [Display(Name = "CurrentPasswordLabel", ResourceType = typeof(Resources.Culture.Account.Account))]
    public string OldPassword { get; set; }

    [Required(ErrorMessageResourceName = "FieldIsRequired", ErrorMessageResourceType = typeof(Resources.Culture.Home.Global))]
    [StringLength(100, ErrorMessageResourceName = "NewPasswordLength", ErrorMessageResourceType = typeof(Resources.Culture.Account.Account), MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "NewPasswordLabel", ResourceType = typeof(Resources.Culture.Account.Account))]
    public string NewPassword { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "ConfirmPasswordLabel", ResourceType = typeof(Resources.Culture.Account.Account))]
    [Compare("NewPassword", ErrorMessageResourceName = "FieldMismatch", ErrorMessageResourceType = typeof(Resources.Culture.Home.Global))]
    public string ConfirmPassword { get; set; }
}
like image 544
user2441279 Avatar asked Apr 15 '26 19:04

user2441279


1 Answers

This looks like a known bug in MVC4, and MVC5 (which I am using). It looks like it has been fixed in 5.1:

http://aspnetwebstack.codeplex.com/workitem/1401

like image 184
Matt Frear Avatar answered Apr 18 '26 09:04

Matt Frear



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!