Steps to reproduce:
ErrorMessage
specified in RegisterViewModel
for the ConfirmPassword
property. It is "The password and confirmation password do not match.".It seems like a custom ErrorMessage
property on the Compare
attribute isn't working. Even if I specify a ErrorMessage, the validation still shows some sort of default message instead.
This work with System.Web.Mvc.CompareAttribute
, but this is now deprecated and you should instead use System.ComponentModel.DataAnnotations.CompareAttribute
, which shows this problem.
I add the Compare
attribute to a property and specify the ErrorMessage
as follows:
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match. I'll even add some random text!")]
public string ConfirmPassword { get; set; }
the expected result would be (but this isn't what I get):
Instead, I get this "default" error message:
Client side validation is disabled. Am I missing something? This example is taken from the MVC 5 template with Individual User Accounts as authentication.
I have same problem, solution:
Change:
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
To:
[System.Web.Mvc.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
It's working!
I think this is a bug. (Edit: It is.) I'm also able to reproduce this. In fact, the behaviour is exactly the same with client-side validation turned on. If you take a look at the generated HTML, you'll see it's not even generating the custom error string - it always emits the default one.
Actually, I've just had a search around to find more information and I've found it has been submitted as a bug on codeplex. It was reported 8 days ago and someone has been assigned to it. You can find the bug report here.
It's an old bug from 2013. Try the following command to update all of the project's dependencies:
PM> update-package
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With