2

I hope the subject made sense :)

I'm not fussed about local/global stuff but if I have a resource named IsRequired with a value of "{0} is required." how to use with Data Annotations? (not MVC btw)

This will "work", of course:

[Required(ErrorMessageResourceName = "IsRequired", 
ErrorMessageResourceType=typeof(Resources))]

But I'd need something like:

[Required(string.Format(ErrorMessageResourceName = IsRequired, "MyProperty"), 
ErrorMessageResourceType=typeof(Resources))]

(yes I know that won't work ;)

Is it possible to format the resource string in a data annotation required attribute? Why write 200 required resource strings when 1 will suffice?

Thanks, Richard

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
Richard
  • 5,810
  • 6
  • 29
  • 36

1 Answers1

1

One option is to subclass RequiredAttribute:

public class MandatoryAttribute : RequiredAttribute
{
    // ...
}

You can then override the Validate method, in which you can inspect the ValidationContext for the property name and use that to produce the correct error message.

Jacob
  • 77,566
  • 24
  • 149
  • 228