2
votes

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

1
I'm already using Enterprise Library 5 VAAB. - Richard

1 Answers

1
votes

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.