3

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; }
}
  • Do you really just want to output the new password to the screen? That's violating several security principles. – Jeroen Vannevel Nov 19 '13 at 00:43
  • No , that's not what he said. There is a ErrorMessageResourceName called "FieldMismatch", which contains the text "The field {0} and {1} do not match". This should be displayed as the message "The field old password and new password do not match". – BdR Nov 19 '13 at 11:51

2 Answers2

2

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

Matt Frear
  • 52,283
  • 12
  • 78
  • 86
0

I have found another approach for this problem. I have all translation made public, build action="Embedded Resource" and custom tool PublicResXFileCodeGenerator. All files are placed in ${MySolution}/App_GlobalResources/Translation*.resx files

[Required(ErrorMessageResourceType = typeof(App_GlobalResources.Translation), ErrorMessageResourceName = "MessageRequired")]
//[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[StringLength(100, ErrorMessageResourceType = typeof(App_GlobalResources.Translation), ErrorMessageResourceName = "MessagePaswdLen", MinimumLength = 6)]
[DataType(DataType.Password)]
//[Display(Name = "New password")]
[Display(ResourceType = typeof(App_GlobalResources.Translation), Name = "Password")]
public string NewPassword { get; set; }

English translation looks like this


MessageRequired|{0} is required
MessagePaswdLen|The {0} must be at least {2} characters long.

and Polish one


    MessagePaswdLen|{0} musi mieć co najmniej  {2} znaków.  
    MessageRequired|{0} jest wymagane


Rafal
  • 209
  • 2
  • 3