Hi I'm just wondering if this could actually be mvc or if it's something im missing!
having a play around and noticed if i try and use a jquery UI date picker with an mvc form and set the date picker's date format to dd/mm/yyyy and save, the mvc unobtrusive validation says no!
I've got the following setup in my program:
1. <globalization culture="en-AU" uiCulture="en-AU" enableClientBasedCulture="false" /> in web.config
2. Model binders for DateTime and DateTime? so that the date will be in my viewmodel in the correct format like so:
ModelBinders.Binders.Add(typeof(DateTime), new LocalModelBinder());
ModelBinders.Binders.Add(typeof(DateTime?), new LocalModelBinder());
public class LocalModelBinder : System.Web.Mvc.IModelBinder
{
public object BindModel(ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
{
//var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
//var date = value.ConvertTo(typeof(DateTime), CultureInfo.CreateSpecificCulture("en-AU"));
//return date;
string dateString = controllerContext.HttpContext.Request.QueryString[bindingContext.ModelName];
var dt = new DateTime();
bool success = DateTime.TryParse(dateString, CultureInfo.GetCultureInfo("en-AU"), DateTimeStyles.None, out dt);
if (success)
{
return dt;
}
else
{
return null;
}
}
}
I saw people posting on forums and blogs re allowing mvc validation to work using globalizing.. is that really what is required to get it up and running?
First of all you don't need at all a custom model binder because the default model binder takes into account the culture you set in the web.config, so if dd/mm/yyyy is a valid date format in the en-AU culture it should pass server side validation.
On the client side, as default the Mvc engine doesnt validate date formats, so validation is performed just if you added manually some jquery validation. If this is the case you must use the globalize library. However the scripts in the post you referred
to doesnt redefine the jquery validation for dates because as I explained as default date validation is not performed on the client side in Mvc.
for a complete solution, that includes automatic client side culture set up and redefinition of date validatio according to the selected culture also on the client side.
owlyowl
Member
2 Points
3 Posts
mvc date time validation failing for dd/mm/yyyy date validation Globalisation required?!
Aug 18, 2012 01:41 PM|LINK
Hi I'm just wondering if this could actually be mvc or if it's something im missing!
having a play around and noticed if i try and use a jquery UI date picker with an mvc form and set the date picker's date format to dd/mm/yyyy and save, the mvc unobtrusive validation says no!
I've got the following setup in my program:
1. <globalization culture="en-AU" uiCulture="en-AU" enableClientBasedCulture="false" /> in web.config
2. Model binders for DateTime and DateTime? so that the date will be in my viewmodel in the correct format like so:
ModelBinders.Binders.Add(typeof(DateTime), new LocalModelBinder()); ModelBinders.Binders.Add(typeof(DateTime?), new LocalModelBinder()); public class LocalModelBinder : System.Web.Mvc.IModelBinder { public object BindModel(ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext) { //var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); //var date = value.ConvertTo(typeof(DateTime), CultureInfo.CreateSpecificCulture("en-AU")); //return date; string dateString = controllerContext.HttpContext.Request.QueryString[bindingContext.ModelName]; var dt = new DateTime(); bool success = DateTime.TryParse(dateString, CultureInfo.GetCultureInfo("en-AU"), DateTimeStyles.None, out dt); if (success) { return dt; } else { return null; } } }3. a data format attribute on my ViewModel
[DataMember] [DisplayName("Raptor Weight Date"), DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{dd-MM-yyyy}")] public DateTime? RaptorWeightDate { get; set; }I saw people posting on forums and blogs re allowing mvc validation to work using globalizing.. is that really what is required to get it up and running?
http://blog.brainnovative.com/2010/12/globalizing-aspnet-mvc-unobtrusive.html
francesco ab...
All-Star
20888 Points
3277 Posts
Re: mvc date time validation failing for dd/mm/yyyy date validation Globalisation required?!
Aug 18, 2012 07:40 PM|LINK
First of all you don't need at all a custom model binder because the default model binder takes into account the culture you set in the web.config, so if dd/mm/yyyy is a valid date format in the en-AU culture it should pass server side validation.
On the client side, as default the Mvc engine doesnt validate date formats, so validation is performed just if you added manually some jquery validation. If this is the case you must use the globalize library. However the scripts in the post you referred to doesnt redefine the jquery validation for dates because as I explained as default date validation is not performed on the client side in Mvc.
Moreover, you must set the client side culture to be the same as the one defined on the server side to avois code duplication. This can be done with simple scripts. Please refer to my blog post: http://www.dotnet-programming.com/post/2011/12/14/Globalization-Validation-and-DateNumber-Formats-in-AspNet-MVC.aspx
for a complete solution, that includes automatic client side culture set up and redefinition of date validatio according to the selected culture also on the client side.
Mvc Controls Toolkit | Data Moving Plug-in Videos