7

DataType ErrorMessage doesn't work. MVC4 DataType ErrorMessage doesn't seem to work. I have this dataannotation Attribute:

[DataType(DataType.DateTime, ErrorMessage = "Invalid date")]
public override DateTime? BirthDate { get; set; }

but client validation return this error:

The field BirthDate must be a date.

this is the Html portion:

 <input Value="" class="date" data-val="true" data-val-date="The field BirthDate  must be a date." data-val-required="El campo Fecha nacimiento es obligatorio" id="Patient_BirthDate" name="Patient.BirthDate" type="text" value="" />

Any idea?

Donald
  • 534
  • 1
  • 10
  • 18
  • possible duplicate of [DataTypeAttribute is showing the wrong message](http://stackoverflow.com/questions/10028384/datatypeattribute-is-showing-the-wrong-message) – archil Dec 17 '12 at 15:40
  • Thank Archil but wath's the mining of ErrorMessage? Also, why the attribute [Required(ErrorMessageResourceType = typeof(Messages.GeneralMessages), ErrorMessageResourceName = "Required")] work properly ? – Donald Dec 17 '12 at 15:48
  • I got it, in MVC 4 is possible to localize default error message. [link](http://weblogs.asp.net/imranbaloch/archive/2013/03/31/localizing-default-error-messages-in-asp-net-mvc-and-web-form.aspxm) – Donald Feb 28 '14 at 14:24

2 Answers2

8

On MVC 4 I was able to change the error message in the View with Html.ValidationMessageFor.
See an example:@Html.ValidationMessageFor(model => model.FechaDesde, "Fecha Inválida")

jmontenegro
  • 301
  • 4
  • 10
  • Problem I've found with this is that it displays the error regardless of whether the data is valid or not. – Reisclef Mar 03 '17 at 13:15
6

Short Answer: Purpose of DataType.DateTime is NOT to validate your DateTime entry for Birthday property. This is the reason. What it does is just formats the DateTime before displaying it on your view.

What you need is to have [Required] attribute on top of that as well.

However, what i usually prefer to use is Jquery Datepicker and it doesn't even allow user to enter any text, but a valid date.

Edit: When you decorate a model property with [DataType(DataType.Date)] the default template in ASP.NET MVC 4 generates an input field of type="date". Browsers that support HTML5 such Google Chrome render this input field with a date picker.

You may enforce this with code:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? BirthDate { get; set; }

In order to correctly display the date, the value must be formatted as 2012-09-28

Yusubov
  • 5,815
  • 9
  • 32
  • 69
  • Ok, it make sense, but wath's the mining of ErrorMessage? – Donald Dec 17 '12 at 15:59
  • When you decorate a model property with [DataType(DataType.Date)] the default template in ASP.NET MVC 4 generates an input field of type="date". Browsers that support HTML5 such Google Chrome render this input field with a date picker. In order to correctly display the date, the value must be formatted as 2012-09-28 – Yusubov Dec 17 '12 at 16:08
  • i have updated my answer with a sample and more explanations. – Yusubov Dec 17 '12 at 16:11
  • 2
    ok, thank you ElYusubov, after dome research i discovered that this message is internal from Framework, so the only solution is to implement a custom validation. – Donald Dec 17 '12 at 21:08