kendoValidator() causes “The field xxx must be a date” error when using DatePicker

Jack picture Jack · Feb 19, 2015 · Viewed 7.2k times · Source

Im my MVC5 application selecting date in "dd/MM/yyyy" format causes "The field xxx must be a date" error. On the other hand, if I comment kendoValidator() line as below the error has gone, but in that case I cannot perform client side validation and for this reason I want to use kendoValidator. Here are the code sections related to this control.

Entity:

[Required(ErrorMessage = "Required field")]
[Display(Name = "Start Date")]
public DateTime StartDate { get; set; }


View:

...    
<script src="~/Scripts/kendo/2014.3.1119/cultures/kendo.culture.de.min.js"></script>
<script src="~/Scripts/kendo/2014.3.1119/messages/kendo.messages.de-DE.min.js"></script>

<script>
    $(function () {
        $("form").kendoValidator(); //This line cause the error...
    });
</script>

@Html.LabelFor(m => m.StartDate )
    @(Html.Kendo().DatePickerFor(m => m.StartDate)
    .Animation(true)
    .Culture("de-DE")
    .Footer(false)
    .Format("dd/MM/yyyy")
    .Value(DateTime.Today) 
 )


Is there any missing or wrong definition on Entity or View? i.e.

[DataType(DataType.Date)]

Thanks in advance for your help...

Answer

David Shorthose picture David Shorthose · Feb 19, 2015

You can try this as a possible solution:

1) If you want to apply the de-DE culture to the entire site then you could add:

<script type="text/javascript">
    kendo.culture("de-DE");
</script>

Substituting the culture for the one you want to apply and also ensuring you have added the appropriate culture script to the page.

2) The next thing you could try is add the date format you want to the allowed date formats "parseFormats" for the control.

This can be done like this:

  @(Html.Kendo().DatePickerFor(m => m.StartDate)
                .Format("dd/MM/yyyy")
                .ParseFormats(new List<string>()               
                              {
                                "dd/MM/yyyy", 
                                "dd/MM/yy",
                                "dd MMM yyyy" 
                              })
   )

These are generally the ways that I go about handling the dates and validating.