I'm using this object:
[Required]
[Display(Name = "AuditDate", ResourceType = typeof(Resources.Audit))]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime dateAudit { get; set; }
In this part of my form:
@Html.TextBox("dateAudit", String.Format("{0:d}", Model.dateAudit.ToString("dd'/'MM'/'yyyy"), new { @class = "form-control" }))
When I try to enter a date, I cannot submit the form (DatePicker dialog appears) because there is trouble with the field. I added a ValidationMessageFor in the field which tells me that the field doesn't contain a valid date:
@Html.ValidationMessageFor(model => model.dateAudit, "", new { @class = "text-danger" })
This is the HTML error message generated at this point for that field:
<input data-val="true" data-val-date="The field Date must be a date." data-val-required="The field Date is required." id="dateAudit" name="dateAudit" type="text" value="10/03/2015" class="hasDatepicker input-validation-error">
I also tried setting the culture for all applications in the config file:
<globalization uiCulture="fr-FR" culture="fr-FR" />
If I select a date like 1st of March, formatted as 01/03/2015
, there's no problem and the model binding recognizes it, but when I select 20th of March, the field fails to validate (I suspect that it thinks I'm trying to enter 20 as the month).
I found this library which could help with client-side validation, but it looks like it isn't compliant with all browsers so I can't use it. I'm considering dropping client-side validation completely and only validating the date field in the back-end.
Thanks for your help.
EDIT: I forget to advice you that I used that jquery ui re-definition for datepicker:
/* French initialisation for the jQuery UI date picker plugin. */
/* Written by Keith Wood (kbwood{at}iinet.com.au),
Stéphane Nahmani (sholby@sholby.net),
Stéphane Raimbault <stephane.raimbault@gmail.com> */
(function (factory) {
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(["../datepicker"], factory);
} else {
// Browser globals
factory(jQuery.datepicker);
}
}(function (datepicker) {
datepicker.regional['fr'] = {
closeText: 'Fermer',
prevText: 'Précédent',
nextText: 'Suivant',
currentText: 'Aujourd\'hui',
monthNames: ['janvier', 'février', 'mars', 'avril', 'mai', 'juin',
'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
monthNamesShort: ['janv.', 'févr.', 'mars', 'avr.', 'mai', 'juin',
'juil.', 'août', 'sept.', 'oct.', 'nov.', 'déc.'],
dayNames: ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'],
dayNamesShort: ['dim.', 'lun.', 'mar.', 'mer.', 'jeu.', 'ven.', 'sam.'],
dayNamesMin: ['D', 'L', 'M', 'M', 'J', 'V', 'S'],
weekHeader: 'Sem.',
dateFormat: 'dd/mm/yy',
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: ''
};
datepicker.setDefaults(datepicker.regional['fr']);
return datepicker.regional['fr'];
}));