Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use resources as ErrorMessage with DataAnnotations?

Why can't I do like this?

[Required(ErrorMessage = "*")]
[RegularExpression("^[a-zA-Z0-9_]*$", ErrorMessage = Resources.RegistrationModel.UsernameError)]
public string Username { get; set; }

What is the error message telling me?

An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type.

like image 908
Johan Alkstål Avatar asked Apr 22 '10 07:04

Johan Alkstål


3 Answers

When you are using the ErrorMessage property only constant strings or string literal can be assigned to it.

Use the ErrorMessageResourceType and ErrorMessageResourceName instead to specity your resources.

[RegularExpression(
    "^[a-zA-Z0-9_]*$", 
    ErrorMessageResourceType=typeof(Resources.RegistrationModel),
    ErrorMessageResourceName= "UsernameError"
)]

Note that the resources must be public (can be set in the resource editor).

Setting resource access to public

like image 122
AxelEckenberger Avatar answered Oct 20 '22 12:10

AxelEckenberger


Please see this link: http://code.msdn.microsoft.com/Getting-Started-WCF-RIA-1469cbe2/sourcecode?fileId=19242&pathId=774666288 (link broken, but left for attribution purposes)

public sealed partial class RegistrationData 
{ 
    [Key] 
    [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ErrorResources))] 
    [Display(Order = 0, Name = "UserNameLabel", ResourceType = typeof(RegistrationDataResources))] 
    [RegularExpression("^[a-zA-Z0-9_]*$", ErrorMessageResourceName = "ValidationErrorInvalidUserName", ErrorMessageResourceType = typeof(ErrorResources))] 
    [StringLength(255, MinimumLength = 4, ErrorMessageResourceName = "ValidationErrorBadUserNameLength", ErrorMessageResourceType = typeof(ErrorResources))] 
    public string UserName { get; set; } 
like image 29
Feng Avatar answered Oct 20 '22 14:10

Feng


Try FluentModelMetaDataProvider.

Managed to use resources for error messages in strongly typed fashion.

Looks like this:

using System.Web.Mvc.Extensibility;

namespace UI.Model
{
    public class StoreInputMetadata : ModelMetadataConfigurationBase<StoreInput>
    {
        public StoreInputMetadata()
        {
            Configure(m => m.Id)
                .Hide();
            Configure(model => model.Name)
                .Required(Resources.Whatever.StoreIsRequired)
                .MaximumLength(64, Resources.Whatever.StoreNameLengthSomething);
        }
    }
}

What is the error message telling me?

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.

It's already self explanatory. C# isn't dynamic language like Ruby where You can write classes that inherits random base class at runtime. :)

Here's what Skeet says about this.

like image 40
Arnis Lapsa Avatar answered Oct 20 '22 13:10

Arnis Lapsa