PrimeFaces 日历接受无效日期作为输入 [英] PrimeFaces calendar accepts invalid dates as input

查看:23
本文介绍了PrimeFaces 日历接受无效日期作为输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是 PrimesFaces 3.4.1 日历.当使用通过按钮或输入字段焦点激活的弹出日期选择器时,您只能选择工作正常的有效日期,快乐的日子!

The problem I am having is with the PrimesFaces 3.4.1 calendar. When using the popup date picker activated either through the button or on input field focus you can only select valid dates which work fine, happy days!

当您在输入字段中手动添加日期时会出现问题,如果您添加无效日期,PrimeFaces 日历组件会对其进行最佳猜测,将其转换为有效日期然后发送,这意味着后端验证是不行.以下是一些有趣的翻译:

The issues comes when you manually add a date into the input field, if you add an invalid date the PrimeFaces calendar component takes its best guess at converting this into a valid date and then sending it, meaning that back-end validation is a no go. Some interesting translations below:

  • 30/02/2012 变成 2/6/2014
  • 322/05/2012 变成 5/10/2038
  • 01/14/2012 变成 4/1/2012

要重现这种疯狂,请查看 PrimeFaces 日历展示一>.

To recreate this madness have a look at the PrimeFaces Calendar Showcase.

我已经看到了使用 readOnlyInput='true' 属性的解决方案,但这似乎只能防止在字段中输入字母,而不是数字或斜线.下面是我实现的日历的一个实例:

I have seen solution around using the readOnlyInput='true' attribute but that only seems to prevent letters being entered in the field not number or slashes. Below is one instance of the calendar I have implemented:

<p:calendar id="fldDateOfBirth"
            value="#{pc_CreateUser.user.dateOfBirth}"
            binding="#{pc_CreateUser.dobComp}"
            navigator="true"
            pattern="dd/MM/yyyy"
            maxlength="10"
            yearRange="-100"
            validator="#{pc_CreateUser.validateDOB}"
            title="#{msg.user_date_format_default_tip}"
            converterMessage="#{msg.user_error_dob_invalid}"
            readOnlyInput="true"
            showOn="button" />

明智的解决方案我愿意接受任何建议:

Solution wise I am open to any suggestions:

  1. 这是 PrimeFaces 中的常见问题吗?有没有我可以使用的技巧解决了吗?
  2. 我可以使用 JavaScript 在发送日期之前验证日期吗?完全阻止所有用户输入?
  3. 还有什么我没想到的!

提前致谢,这已经给我带来了数周的问题!

Thanks in advance, this has been causing me issues for weeks!

推荐答案

在幕后使用 SimpleDateFormat 依次默认使用 宽松 解析,导致溢出的值转入下一个日期指标级别.例如.1 月 32 日将变为 2 月 1 日,依此类推

The <p:calendar> uses under the covers SimpleDateFormat which in turn uses by default lenient parsing, causing the overflowed values to roll over into the next date metric level. E.g. 32 January would become 1 February, etc.

用简单的 Java 术语来说,这可以通过 DateFormat#setLenient(),传递false.另请参阅此问题:使用日期格式验证日期.

In plain Java terms, this can be turned off by DateFormat#setLenient(), passing false. See also among others this question: validating a date using dateformat.

在 JSF 术语中,您基本上需要提供一个使用非宽松 DateFormat 的自定义转换器.幸运的是,标准 JSF 已经提供了这样一种开箱即用的 ,所以你可以直接使用它.

In JSF terms, you basically need to provide a custom converter which uses a non-lenient DateFormat. Fortunately, standard JSF already provides such one out the box in flavor of <f:convertDateTime>, so you could just make use of it directly.

<p:calendar ...>
    <f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>

这篇关于PrimeFaces 日历接受无效日期作为输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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