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

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

问题描述

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



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




  • 30/02/2012成为2/6/2014

  • 322 / 05/2012 become 5/10/2038

  • 01/14/2012即成4/1/2012



要重现这个疯狂,请查看 PrimeFaces日历展示



我已经使用 readOnlyInput ='true'属性看到了解决方案,以防止在字段中输入的字母不是数字或斜杠。下面是我实现的日历的一个实例:

 < 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/>

解决方案我明白任何建议:


  1. 这是PrimeFaces中的常见问题吗?

  2. 我可以使用JavaScript来验证发送之前的日期,也可以使用
    来阻止所有用户输入?

  3. 我还没有想过的任何事情。

>在封面下使用 SimpleDateFormat ,默认情况下会使用宽松解析,导致溢出的值滚动到下一个日期度量标准级别。例如。



在纯Java语言中,这可以通过 DateFormat#setLenient() ,传递 false 。另请参阅此问题:使用dateformat验证日期



在JSF术语中,您基本上需要提供一个使用非宽松 DateFormat 的自定义转换器。幸运的是,标准的JSF已经提供了这样一个开箱即化的 < f:convertDateTime> ,因此您只需直接使用它即可。

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


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!

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 becomes 2/6/2014
  • 322/05/2012 becomes 5/10/2038
  • 01/14/2012 becomes 4/1/2012

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

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. Is this a common issues in PrimeFaces? Is there a trick I can use to fix it?
  2. Could I use JavaScript to validate the date before it's sent or to block all user input entirely?
  3. Anything else I haven't thought of!

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

解决方案

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.

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.

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天全站免登陆