14

I can't localize validate: 'Confirm password' and 'Password' do not match. in MVC5

[Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] //Why not display this message???????
        public string ConfirmPassword { get; set; }

Please help me localize it.

vietdv
  • 345
  • 1
  • 3
  • 11

3 Answers3

14

You have 2 options to solve this bug:

--Option 1

Change:

[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]

to

[System.Web.Mvc.Compare("Password", ErrorMessage = "Your custom error message")]

--Option 2 (I recommend this one)

We need to update our ASP.NET MVC 5. In your Visual Studio go to the Package Manager Console and type:

PM> update-package

You migh get an error in the:

public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }

That error is caused by the update in the internal structure of MVC 5. To solve that error do this: https://stackoverflow.com/a/23090099/2958543

Community
  • 1
  • 1
mejiamanuel57
  • 6,807
  • 1
  • 32
  • 34
6

It appears that it is a known issue and is not working correctly at the moment - http://aspnetwebstack.codeplex.com/workitem/1401.

However a temporary workaround would be using the Compare attribute from System.Web.Mvc, which is marked obsolete. Here is an example:

using CompareObsolete = System.Web.Mvc.CompareAttribute;

...

[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[CompareObsolete("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }

I am currently using this workaround until the official fix is available. Everything is working perfectly fine - I am using this attribute to localize error messages using Resources.

Just don't forget to update it once the official fix comes out.

EDIT: The issue has been fixed in the latest release.

Vasil Dininski
  • 2,368
  • 1
  • 19
  • 31
2

There seems to be two types of CompareAttribute. Looking at MSDN, the one with namespace System.Web.Mvc seems to be obsolete and they suggest using the one with namespace System.ComponentModel.DataAnnotations. Link: https://msdn.microsoft.com/en-us/library/system.web.mvc.compareattribute(v=vs.118).aspx

With Visual Studios, you'll have to be explicit with the annotation and add the namespace to attribute in the annotation like the following:

[System.ComponentModel.DataAnnotations.CompareAttribute("Password", ErrorMessage = "The password and confirmation password do not match.")]

For more information, see also: System.ComponentModel.DataAnnotations.compare vs System.Web.Mvc.Compare

Community
  • 1
  • 1
Lem
  • 61
  • 1
  • 6