20
votes

Steps to reproduce:

  1. Create a new MVC 5 project with Individual User Accounts.
  2. Look at the ErrorMessage specified in RegisterViewModel for the ConfirmPassword property. It is "The password and confirmation password do not match.".
  3. Build and run the application, and try to register with non-matching passwords. I'll se the error message "'Confirm password' and 'Password' do not match.", instead of the one specified in the model.

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):

Expected result with custom ErrorMessage

Instead, I get this "default" error message:

Actual result with standard ErrorMessage

Client side validation is disabled. Am I missing something? This example is taken from the MVC 5 template with Individual User Accounts as authentication.

4
Why are you sure it's disabled? And how it's related to the result you are expecting? - Agat
I assume you mean client side validation? I've changed the web.config to <add key="ClientValidationEnabled" value="false" /> and if I check the Net panel in my developer tool I can see the postback, and the error message in the response HTML. - JLe
So, what's the problem? You say that you've added the validation, you expect it to be as on the picture, but you are not happy with that. - Agat
I show a picture of how I expect it to be. What I get is shown on the picture below, which isn't what I expect. I will clarify that. - JLe
I think you did not search well, the same question were [here][1] Let me know if that was helpful. [1]: stackoverflow.com/questions/4938078/… - NativeBasic

4 Answers

16
votes

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!

8
votes

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.

7
votes

It's an old bug from 2013. Try the following command to update all of the project's dependencies:

PM> update-package
-1
votes

Use System.ComponentModel.DataAnnotations.Compare instead. Since the System.Web.Mvc.Compare is obsolete,the above compare attribute does the same work.