1
Vote

Using Resource File Error Messages Fails With Data Annotations Attributes

description

Porting from https://aspnet.codeplex.com/workitem/8175:

The additional data annotations attributes included in MVC 3 Futures (CreditCard, EmailAddress, FileExtension, Url) do not work when using a Resource File Property. For example, using the attribute [Email(ErrorMessageResourceName = "ErrorMessage", ErrorMessageResourceType = typeof(Resource1))] fails at runtime with the following error: "Either ErrorMessageString or ErrorMessageResourceName must be set, but not both". This occurs because a default ErrorMessage is being set in the attribute constructors, so when the user adds a resource error message the FormatErrorMessage() base class method doesn't know which to use. I believe the fix would be to take the default error message out of the constructor and override the FormatErrorMessage() method of the base class, and augment it by setting the default ErrorMessage only if no message (either a static string or resource file) has been provided.

public override string FormatErrorMessage(string name)
{
if (ErrorMessage == null && ErrorMessageResourceName == null)
{
ErrorMessage = ValidatorResources.CreditCardAttribute_Invalid; //default error
}
return base.FormatErrorMessage(name);
}


comments



notworking wrote Sep 2, 2012 at 9:41 AM


[EmailAddress(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "email_not_valid", ErrorMessage = null)]
can help until issue not resolved



RonaldEvers wrote Oct 19, 2012 at 4:43 PM


Sorry, your suggested solution ('notworking wrote Sep 2 at 6:41 PM') doesn't work at my side.



danroth27 wrote Thu at 10:12 AM


Still repros on MVC 5

comments

yishaigalatzer wrote Dec 13, 2013 at 10:39 PM

Thanks for this report. Although very valid, we stopped shipping MVC futures, and the parallel code is in the .NET framework.

We notified the right team, and if you want to follow up, you can open a connect bug.

The work around is to override the FormatErrorMessage and implement getting the resource directly (look at similar attributes for an example)



** Closed by yishaigalatzer 12/13/2013 2:39PM

eilonlipton wrote Jan 8 at 12:57 AM

Re-opening and moving to the Breaking Change milestone so that we can investigate this again when we can do larger breaking changes and take over some of the Data Annotations attributes in Diego's team.