Kendo DatePicker 无法验证自定义日期格式 [英] Kendo DatePicker fails validation for custom date format

查看:29
本文介绍了Kendo DatePicker 无法验证自定义日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Kendo DatePicker 编辑显示在我的 ASP.NET MVC 4 项目的 Kendo 网格中的日期字段.为了让用于日期字段的 DatePicker 使用自定义日期格式字符串,我将 EditorTemplates 文件夹下的 Date.cshtml 文件更新为以下内容:

I'm using Kendo DatePicker to edit a Date field being displayed in a Kendo Grid in my ASP.NET MVC 4 project. In order to have the DatePicker being used for the Date field use custom date format string, I updated the Date.cshtml file under the EditorTemplates folder to the following:

@(Html.Kendo().DatePickerFor(m => m).Format("dd/MM/yyyy"))

通过这样做,我设法让 DatePicker 显示我想要的格式.但是,它无法通过手动键入或从弹出式日历中选择输入的某些日期进行验证.

By doing that, I managed to have the DatePicker display the format as I want it to. However, it failed validation for some of the dates entered for input, either via manual key in or selection from the popup calendar.

经过进一步调查,我可以说 DatePicker 正在根据 M/d/Y 格式验证日期.这个假设是基于我的成立,即 12/1/2012 是一个有效日期,而 13/1/2012 不是.

Upon further investigation, I can say that the DatePicker is validating the date based on a M/d/Y format. That assumption was made based on my foundings that 12/1/2012 is a valid date, whereas 13/1/2012 is not.

我还尝试将 .ParseFormat("dd/MM/yyyy") 添加到 Date.cshtml 中 DatePicker 声明的末尾,但它没有解决任何问题.所以我会说这绝对是一个错误,我稍后会向 Telerik 报告.

I also tried adding .ParseFormat("dd/MM/yyyy") to the end of the DatePicker declaration in Date.cshtml but it does not fix anything. So I would say that this is definitely a bug and I will report this to Telerik later.

但就目前而言,我正在寻找一种解决方法来解决这个问题.我发现我可以在 Javascript 中覆盖 kendo.ui.validator.rules.mvcdate 以获得我自己的验证功能.虽然这在 Chrome 中工作正常,但它在 IE9 中不起作用.

But for the time being, I'm looking for a workaround to have this working. I find that I can override kendo.ui.validator.rules.mvcdate in Javascript to have my own validation function. While this work fine in Chrome, it does not work in IE9.

那么,有什么想法可以让 DatePicker 接受 dd/MM/yyyy 输入格式?提前致谢.

So, any ideas how I can make the DatePicker to accept dd/MM/yyyy input format? Thanks in advance.

推荐答案

在内部,ASP.NET MVC 的日期验证规则(不显眼的客户端验证),使用 kendo.parseDate(string) 方法,内部将使用 预定义的日期模式.我想在您的情况下,默认文化是en-US",这就是验证失败的原因,因为具有dd/MM/yyyy"格式的日期被认为是无效的.一种可能的解决方案是覆盖日期验证规则(如您所做的那样)并使用特定格式解析字符串.另一个选项是为页面设置差异文化设置.例如,de-DE"文化的短日期格式是dd/MM/yyyy".

Internally, the date validation rule for ASP.NET MVC (the unobtrusive client validation), uses kendo.parseDate(string) method, which internally will use the predefined date patterns if no format/s is/are defined. I suppose that in your case the default culture is "en-US" and that is why validation fails, because dates with "dd/MM/yyyy" format are considered as not valid. One possible solution is to override the date validation rule (as you did) and parse the string using a specific format. The other option is to set diff culture settings for the page. For instance, the short date format for the "de-DE" culture is "dd/MM/yyyy".

这篇关于Kendo DatePicker 无法验证自定义日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆