tags:

views:

809

answers:

5

Hi! I have ValidationAttribute like:

    public class Username : ValidationAttribute
 {
  public override bool IsValid(object value)
  {
   if (value == null)
    return false;

   return RegExp.Validate(RegExpLib.Username,value.ToString());
  }
 }

..and using it like this:

    public class AccountSignIn
 {
  [Username(ErrorMessageResourceName ="txtUsername",ErrorMessageResourceType=typeof(SignIn))]
  public string Username { get; set; }

  public string Password { get; set; }

  public bool RememberMe { get; set; }

  public string ReturnUrl { get; set; }
 }

SignIn is resource file at App_GlobalResources and "txtUsername" - is string name in resource file.

Problem: Error messages are not shown.

Question: How to set error message if I have few languages on the website.

Another info: Im able access SignIn.txtUsername from Views or any file in the project. From controller ModelState.AddModelError("Username", Resources.SignIn.txtUsername); works fine as well.. I can assign ErrorMessage inside ValidationAttribute, but got error after second validation try... if I place check like this - if(ErrorMessage != Resources.SignIn.txtUsername) ErrorMessage = Resources.SignIn.txtUsername; - I have error after I switch to another language - ErrorMessage can be assigned only once.

Please advice how I can solve it.

Thank you in advance.

A: 

Make sure in the resource editor that your resource visibility is set to "Public" and not "Internal". Otherwise the reflection done by DataAnnotations won't work.

Brad Wilson
As I wrote I'm using Global resources "SignIn is resource file at App_GlobalResources"
Unfortunately, you can not make resources in App_GlobalResources public. You have to use resource file from "Properties" folder instead - right click on project, select properties, from left menu select "Resources" and click "this project...". The default resource file named "Resources.resx" will be created. You can rename it(ie "ModelErrors") and use in code: [Required(ErrorMessageResourceName = "Password_Required", ErrorMessageResourceType = typeof(Properties.ModelErrors))]
Feryt
Yep, that's true. DataAnnotations exist to be used outside of ASP.NET, so they don't know anything about App_XxxResources and just work with traditional resource files.
Brad Wilson
A: 

any update regarding it? can't believe nobody builds localized apps..

vlad
+1  A: 

I had some problems with using localized error messages and DataAnnotations, pretty much identical to your situation. Although I didn't end up with blank being displayed as error messages, I always got some exceptions thrown. Solution for me was:

  1. in App_GlobalResources/Errors.resx open the resource file, change it's properties to Build Action: Embedded Resource, Custom Tool: PublicResXFileCodeGenerator, Custom Tool Namespace: Resources (visual studio does something weird in autogenerating these, so check back again that things are ok)

  2. Change the resource file's access modifier to Public

  3. Check from the code file generated (Errors.Designer.cs) that the namespace and access modifier are correct.

  4. Try it

My example from the Model class:

[Required(ErrorMessageResourceType = typeof(Resources.Errors), ErrorMessageResourceName="ResponseMessageRequired")]
public string message { get; set; }
Lauri Larjo
A: 

Lauri Larjo, Thank you very much for your help! It's works (finally).

vlad