0

My ValidationAttributes don't access my ErrorMessages value-pairs stored in a .resx Resource File generated with VS2010 built in resource generator. (Add New Item - Resources File)

I simply added a folder named Resources, then added a new Resources File and called it ErrorResources, in the resources editor I added a value pair StringLengthError - Too Long**.

Then in my DataAnnotation.ValidationAttribute I wrote:

  [StringLength(5, MinimumLength = 3, 
                    ErrorMessageResourceType = typeof(ErrorResources),
                    ErrorMessageResourceName = "StringLengthError")]
  public string Details { get; set; }

I added a using statement to include the ErrorResources.

But when I run it, the ValidationAttribute StringLength is totally void.

If I use it without the ErrorResources, as in :

  [StringLength(5, MinimumLength = 3)]
  public string Details { get; set; }

The validation attribute comes back to life.

I tried accessing the ErrorResources value pairs in other part of the code, and it works. I just can't get my ValidationAttributes to use the ErrorResources resource.

Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
Rafael
  • 2,413
  • 4
  • 32
  • 54

2 Answers2

0

Try change Access Modifier setting of Resources File to public !

enter image description here

Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
Bohdan Lyzanets
  • 1,652
  • 22
  • 25
  • Hi: I forgot to say that I already did that, and nothing happened! Furthermore, there's a project that I downloaded (some article that explains ValidationAttributes) that uses this kind of error messages in resources, it happened that the resources defined by this guy work, but I created a resource file in this project, and my resources didn't work in that project. I don't know why, could it be something related to the culture, but if so, why didn't it work in that project?, I don't kno what to think. This is the project: https://github.com/jeffhandley/RIAServicesValidation – Rafael Sep 05 '12 at 01:56
  • If I use the resouce files that are already present in that project, then it works, but defining new resource files, even in the same directoy, don't work – Rafael Sep 05 '12 at 01:58
0

Access modifier should be Public but that's not enough. You will have to change properties of your resx file to look like this:

Properties

Pay attention to Build Action and Custom Tool

In addition Resx used by data annotations should sit in App_GlobalResources rather than in Resources you've mentioned. Try these, see if that works alright.

Hope this will help you.

Display Name
  • 4,672
  • 1
  • 33
  • 43
  • Yes that's the answer, Custom Tool is also important. – Rafael Sep 24 '12 at 14:59
  • @user1353914 If that's the answer that was most useful and helpful, please mark it as accepted one. Thanks to you too . – Display Name Sep 24 '12 at 15:29
  • You seem to be very new to stackoverflow.com. Welcome & Enjoy & Good luck. You *click on V mark* (it says "click to set this answer as your accepted answer"). – Display Name Sep 24 '12 at 23:39