1

I am using JSF,Primefaces.

I have a search Criteria Functionality in my App, there i am facing a problem with calendar date.

I am using this piece of code for my application.

<p:calendar id="Date" value="#{bean.fromDate}" pattern="MM/dd/yyyy" showOn="button"/>

I faced this sort of issue that it is accepting even i had given the date 214/03/2013

as and then converting it into 10/03/2030 and performing the action!

But,What exactly i need to do is either to display a message or else to mask that input field as 99/99/9999.

I referred this Link

In my scenario i should not use readonlyInput="true" because i may or may not give a date.

If i am using that then i am unable to clear the field back to empty,because i am in use with @SessionScoped.

Community
  • 1
  • 1
09Q71AO534
  • 4,300
  • 13
  • 44
  • 68

1 Answers1

1

Having a look to how Primefaces Calendar works, it turns to use by default Java's SimpleDateFormat to do the parsing work. If you have a look at the docs, Java uses heuristics to force any given input to convert to an specific date.

Specify whether or not date/time parsing is to be lenient. With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format. With strict parsing, inputs must match this object's format.

That's why your calendar input is accepting that weird dates when they don't even exist. The key is to provide the calendar your custom converter which doesn't accept a lenient date format. JSF already provides one:

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

That would throw a conversion error if the end user provides a non-existing date.

See also:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • Thanks 4 the Answer :-) – 09Q71AO534 Dec 04 '13 at 07:56
  • if i am giving date as `12/12/201345` it is not showing any error message. My Code is ` ` – 09Q71AO534 Dec 04 '13 at 10:07
  • The fact is that even the pattern covers four characters for the year field, `201345` is actually a valid year, so the jsf data parser parses it. What it would be incorrect is to accept 13 as a month or 32 as a day. Anyway, you can also use your custom converter/validator to accept only 4 digit years. – Aritz Dec 04 '13 at 10:56