开发者

Emit DataAnnotations [DisplayName] in MVC PropertyValueRequired format string?

Desired behavior

An undecorated non-nullable value-type property in a model automatically gets a validation message "A v开发者_开发知识库alue 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.


Works for me. Steps:

  1. In App_GlobalResources/Messages.resx add Required => Information is required for {0}
  2. Decorate your model:

    public class MyViewModel
    {
        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Messages))]
        [DisplayName("Bar")]
        public string Foo { get; set; }
    }
    
  3. In the view leave the corresponding field empty:

    @using (Html.BeginForm())
    {
        @Html.LabelFor(x => x.Foo)
        @Html.EditorFor(x => x.Foo)
        @Html.ValidationMessageFor(x => x.Foo)
        <input type="submit" value="OK" />
    }
    
  4. If you submit the form leaving empty the following messages is displayed: Information is required for Bar


The answer is MVC3 does not support this. From the MVC source:

PropertyValueInvalid

string displayName = propertyMetadata.GetDisplayName();
string errorMessageTemplate = GetValueInvalidResource(controllerContext);
string errorMessage = String.Format(CultureInfo.CurrentCulture, errorMessageTemplate, modelState.Value.AttemptedValue, displayName);
modelState.Errors.Remove(error);
modelState.Errors.Add(errorMessage);

PropertyValueRequired

bindingContext.ModelState.AddModelError(modelStateKey, GetValueRequiredResource(controllerContext));

I didn't look further, but I suppose it is probably related to lack of model member access in some missing value scenarios.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜