2

i'm a student and beginner developer.

I'm on a project on my workplace.. and I'm using Data Annotations for validation... but I need to use Resources.Messages to implement dynamic error messages. Here goes an example of what I'm trying to do:

[Required (ErrorMessage = (string.Format(Messages.MissingParameter, Messages.Ad)))]
    [Range(0, long.MaxValue)]
    public long? Ad { get; set; }

In this scenario, missing parameter is the follow string "The parameter {0} is required" and Messages.Ad is a string with the internacionalized field.

Soo, if I use Data Annotation, there's a few solutions, like using the ErrorResource... (like this RegularExpressions example -> Why can't I use resources as ErrorMessage with DataAnnotations?)

But these don't contemplate the issue of passing strings that use parameters to deliver the final string (as illustrated on string.format)

So, my question is: How to use complex internationalization string's with data Annotations.

Community
  • 1
  • 1

1 Answers1

1

You can't because you have to use specific values that are able to be used as attribute parameters.

From MSDN - Attributes Tutorial C# :

Attribute parameters are restricted to constant values of the following types:

  • Simple types (bool, byte, char, short, int, long, float, and double)
  • string
  • System.Type
  • enums
  • object (The argument to an attribute parameter of type object must be a constant value of one of the above types.)
  • One-dimensional arrays of any of the above types
toadflakz
  • 7,764
  • 1
  • 27
  • 40