20

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.

JLe
  • 2,835
  • 3
  • 17
  • 30
  • Why are you sure it's disabled? And how it's related to the result you are expecting? – Agat Nov 14 '13 at 13:14
  • I assume you mean client side validation? I've changed the web.config to `` 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 Nov 14 '13 at 13:18
  • 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 Nov 14 '13 at 13:33
  • 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 Nov 14 '13 at 13:37
  • I think you did not search well, the same question were [here][1] Let me know if that was helpful. [1]: http://www.stackoverflow.com/questions/4938078/using-dataannotations-to-compare-two-model-properties – NativeBasic Nov 14 '13 at 14:04
  • @NativeBasic As you can see I **am** using that solution (although not the `Compare` from `System.Web.Mvc` since it deprecated) but it doesn't wok the way I would expect. And yes, I did search. – JLe Nov 14 '13 at 14:21
  • I am not sure what's the issue: you should provide then all the details on your views, controllers and modesl, I guess. I tried the both way: with client or server validations and both work well with a correct message. (Try to build the app from scratch and then compare what's the difference (if the "scratch" works). Also, you may have a look at this (not sure if that might be related to depricated infrastructure): http://connect.microsoft.com/VisualStudio/feedback/details/727650/system-web-mvc-compare-ambiguous-reference-exception – Agat Nov 14 '13 at 14:59
  • @Agat Thanks for your input. However, I just tried to create a new MVC5 project with VS2013 and the issues appears. I've edited my post to describe what I did and how I tried it. The link you provided points to use the deprecated `System.Web.Mvc.CompareAttribute`, which is the deprecated one I'm trying to get away from. (All other attributes used in MVC nowadays is in `System.ComponentModel.DataAnnotations`, so I'd like to use the Compare attribute from there as well. – JLe Nov 15 '13 at 19:02

3 Answers3

16

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!

vietdv
  • 345
  • 1
  • 3
  • 11
  • 8
    Yeah I know, but that seems like a bad workaround since `System.Web.Mvc.Compare` is deprecated. – JLe Nov 15 '13 at 18:52
8

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.

John H
  • 14,422
  • 4
  • 41
  • 74
7

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

PM> update-package
VahidN
  • 18,457
  • 8
  • 73
  • 117