PrimeFaces calendar accepts invalid dates as input
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>
JonnyIrving
My name is Jonathan Irving and I'm currently studying Business Information Technology Bsc (hons) at Northumbria University in Newcastle-upon-Tyne. I'm in my 3rd of 4 years, this is a placement year I'm spending with the lovely people at Durham County Council working with website design and application development for schools. I love all things technical, especially the web. I'm mainly teaching myself the skills needed for website design and development and find myself standing at the bottom of a very tall mountain. The next step is to get some freelancing work to build up a portfolio before I finish Uni and have to venture out into the big wide world.
Updated on August 04, 2022Comments
-
JonnyIrving almost 2 years
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:
- Is this a common issues in PrimeFaces? Is there a trick I can use to fix it?
- Could I use JavaScript to validate the date before it's sent or to block all user input entirely?
- Anything else I haven't thought of!
Thanks in advance, this has been causing me issues for weeks!
-
JonnyIrving over 11 yearsThis worked a treat! Thank you for the solution and your explanation, much appreciated.
-
VeenarM about 10 yearsIf using older version of primefaces using this convertDateTime causes the conversion to return null, and you'll get javascript errors. (getDate == null). If you can upgrade to PF 4 its great unfortunately I cannot and am looking into alternatives :)
-
VeenarM about 10 yearsHow is this helpful? you don't even tell them what you're doing. All you're doing is copying the current already standard jsf datetime converter.