2

Presuming we have a model like so:

public class TheViewModel
{
    public string DateTime? Image_Date { get; set; }
}

And it is added to a Razor view like so:

Html.TextBoxFor(model => model.Image_Date)

Then the following is rendered in the browser:

<input data-val="true" data-val-date="The field Image_Date must be a date." id="Image_Date" name="Image_Date" type="text" value="" />

The attribute data-val-date is what I'm interested in. It's plainly being injected in by MVC's "unobtrusive" jQuery validation integration.

So, what data annotation will override the verbiage in the HTML attribute?

For instance, [Required(ErrorMessage="This field is required!")] will override the standard "The field {0} is required." message.


Failed attempts:

  1. [DataType(DataType.Date, ErrorMessage = "Must be a valid date.")] doesn't appear to do anything to client-side validation.

  2. [DisplayName("...")] changes the wildcard portion of the pattern, but obviously doesn't affect the pattern itself.

Sparky
  • 98,165
  • 25
  • 199
  • 285
Jesse Hallam
  • 6,794
  • 8
  • 48
  • 70
  • Sounds like you might need to fix it manually https://stackoverflow.com/questions/30510221/how-to-fix-the-field-must-be-a-date-on-a-datetime-property-in-mvc – Erresen Jan 19 '18 at 21:57

1 Answers1

3

The data-val-date attribute is added by the framework because the property is type of DateTime?. Its the GetUnobtrusiveValidationAttributes() method of the HtmlHelper class which actually generates all the data-val-* attributes.

Note that [DataType(DataType.Date, "...")] is an attribute used by the EditorFor() method to add the type="date" attribute which in turn generates the browsers HTML-5 datepicker (if its supported by the browser) and is not related with client side validation.

The default error messages are defined in Resource files, and you can create you own to override the defaults.

Create a (say) MyResources.resx in the App_GlobalResources folder (you may need to create this folder) and add the following FieldMustBeDate key and your message (the default message is shown below)

FieldMustBeDate : The field {0} must be a date

and add the following in the Application_Start() of Global.asax

ClientDataTypeModelValidatorProvider.ResourceClassKey = "MyResources";
DefaultModelBinder.ResourceClassKey = "MyResources";

Note you can also override the default error message for the [Required] attribute using the PropertyValueRequired key

  • I'm going to go ahead and mark this answered since this appears to be correct, but can you show how you found `FieldMustBeDate`? – Jesse Hallam Feb 05 '18 at 20:48
  • 1
    Its defined in the [MvcResources.resx](https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/Properties/MvcResources.resx) (line 445) and accessed via [ClientDataTypeModelValidatorProvider.cs](https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/ClientDataTypeModelValidatorProvider.cs). (line 97) ASAIK, their are 4 defaults you can override - `FieldMustBeDate`, `FieldMustBeNumeric`, `PropertyValueInvalid` and `PropertyValueRequired` –  Feb 05 '18 at 21:36
  • I've had no joy getting the default required message overriden. Are you certain about `PropertyValueRequired`? – Jesse Hallam Feb 06 '18 at 19:15
  • @Kivin, Almost certain, but I think your need an additional setting in your `Global.asax` - `DefaultModelBinder.ResourceClassKey = "MyResources";` because `PropertyValueInvalid` and `PropertyValueRequired` are defined in a different namespace (`System.ComponentModel.DataAnnotations`). I'm on leave at the moment so cant confirm at the moment, but found [this answer](https://stackoverflow.com/questions/6214066/how-to-change-default-validation-error-message-in-asp-net-mvc) with a bit more info –  Feb 09 '18 at 01:30