Desired behavior
An undecorated non-nullable value-type property in a model automatically gets a validation message "A value is required" added by DefaultModelBinder. I'd like it to instead display "A value is required for {Property DisplayName}".
I can get this to work exactly like this, for DataAnnotated properties, using a string.format
template. I can also change the default literal string to display from DefaultModelBinder in this circumstance. I've added a templated string as a resource: PropertyValueRequired -> "Information is required for {0}"
, as per:
Asp.Net MVC 2 - Changing the PropertyValueRequired string
Changing the default ModelState error messages in ASP.NET MVC 3
Examples here on StackOverflow indicate it is possible, both for MVC DefaultModelBinder and for DataAnnotations Validator. In both links above, the asker indicates he's doing it for DefaultModelBinder (but can't get other aspects working).
Observed behavior
Unfortunately, it outputs the {0}
verbatim from MVC DefaultModelBinder (where I instead want the DisplayName inserted).
Are the posts above misleading? Does MVC3 support a format string for PropertyValueInvalid but not PropertyValueRequired? By convention, should I only be using DataAnnotation, and if I see the DefaultModelBinder message does it mean I haven't handled/decorated sufficiently?
I think, in the following post, Darin Dimitrov may be saying that it isn't possible to use a template. Quote: "override the default required error message ... globally, which would be useless."
ASP.NET MVC - Custom validation message for value types
Why I want to do this (prompted by an answerer asking)
- When I bind to a built-in complex type, like a
Dictionary<string, int>
, there is no way to decorate the (int)Value with a validation message, so I get a generic validation message (DefaultModelBinder's, not DataAnnotation's) for each member of the dictionary. - I expected (naturally, I think) the localized value-required resource message parsing to work similarly regardless of which layer catches it.
- I disabled
AddImplicitRequiredAttributeForValueTypes
as a popular method to address checkbox and date/time binding issues I was having, which seems to exacerbate this functional disparity. - Seeing the names in a section validation message of errant undecorated fields helps reduce debug time.