0

We've inherited an application and have a query on using @Html.DropDownListFor

@Html.DropDownListFor(model => model.SomeLookup.Id
, new SelectList(SomeList, "Id", "Description")
, "-- select --")

The data-val-required attribute by default is set on the rendered control to

data-val-required="The Id field is required."

This overrides any validation scripting.

We are aware that we can set the data_val_required attribute to the message we want like so:

@Html.DropDownListFor(model => model.SomeLookup.Id
, new SelectList(SomeList, "Id", "Description")
, "-- select --"
, new [] { data_val_required="This field is required" })

The problem is there are dozens of dropdowns across multiple pages within the application (and potentially other applications) which we need to set the attribute on. If we forget to to do this, the user gets the default message which they find confusing.

Is it possible to override this default so that wherever this is used, the default message used is "This field is required"?

mattpm
  • 1,310
  • 2
  • 19
  • 26
  • 1
    Refer [this answer](https://stackoverflow.com/questions/48349290/which-data-annotation-attribute-creates-this-validation-attribute/48350337#48350337) - you need to create a resource file to override the default if you do not want to add your own `[Required(ErrorMessage = "...")]` attributes –  Apr 13 '18 at 01:44
  • 1
    As a side note, your `default(int)` argument in the `SelectList` constructor is pointless - its ignored - the selected option is set based on the value of your `Id` property –  Apr 13 '18 at 01:50
  • "As a side note, your default(int)...". I've updated the sample so this is no longer a distraction. – mattpm Apr 13 '18 at 02:11
  • 1
    Just delete the 4th argument - its pointless :) –  Apr 13 '18 at 02:12
  • Done :) Good to know! I've added a resource file Resources.resx file to a newly created App_GlobalResources folder and have added DefaultModelBinder.ResourceClassKey = "Resources"; in the Global.asax Application_Start() method. But unsure what the property name needs to be, have tried DefaultModelBinder_ValueRequired, PropertyValueRequired and even data_val_required. Will keep hunting, but if you know off the top of your head, that would handy. – mattpm Apr 13 '18 at 02:41
  • The key needs to be `PropertyValueRequired` and the value would be `This field is required` (you mentioned you tried that, so not sure why its not working for you - it works fine for me) –  Apr 13 '18 at 03:06
  • Yes I have that in there. I'm using ASP.NET.MVC 5 - would that have any bearing? – mattpm Apr 13 '18 at 03:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168885/discussion-between-stephen-muecke-and-mattpm). –  Apr 13 '18 at 03:36

1 Answers1

1

The default message is defined in the System.ComponentModel.DataAnnotations resource file. To override it you can create a custom DataAnnotationsModelValidator by inheriting from RequiredAttributeAdapter.

Start by creating a resource file in the App_GlobalResources, say MyResources.resx and add the following key/value (ensure the access modifier is public)

PropertyValueRequired This field is required

Then create the following adaptor

public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
    public MyRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute)
        : base(metadata, context, attribute)
    {
        attribute.ErrorMessageResourceType = typeof(App_GlobalResources.MyResources);
        attribute.ErrorMessageResourceName = "PropertyValueRequired";
    }
}

and finally register the adapter in Global.asax.cs

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(RequiredAttribute), typeof(MyRequiredAttributeAdapter));