4
votes

I am using primefaces calendar but i am allowed to enter invalid date.For e.g. enter date as 32-06-2012 in the input box for date field and save the record. It is saving the date as is saving the date as 02-07-2012. Same behavior can be observed in showcase of primefaces also.

Reference : http://www.primefaces.org/showcase/ui/calendarBasic.jsf

Here is my code

<p:calendar id="copyStartDateCalendar" pattern="dd/MM/yyyy"

         mode="popup" showOn="button" size='8' >

                <f:convertDateTime pattern="MM/yyyy" />

</p:calendar>

What should be done as there seems to be some error with the component itself.

Thanks & Regards

Tarun Madaan

3

3 Answers

2
votes

I had similar problems with the primefaces calendar.

For one it accepts dates with two digits though a pattern of pattern="dd.MM.yyyy" is set. Like 20.06.12 will be shown in the calendar popup as 20.06.2012 misleading the user to think the date was correctly recognized. But the year 12 is actually set.

Anyways, I ended up setting a <f:validator> inside the <p:calendar> like this:

<p:calendar value="#{abschnittDView.bogen.pruefungsDatum}
    mode="popup" locale="de" pattern="dd.MM.yyyy" required="true"
    requiredMessage="Please provide a date."
    converterMessage="Date is invalid.">

    <f:convertDateTime type="date" pattern="dd.MM.yyyy" 
        timeZone="Europe/Berlin" locale="de" />

    <f:validator validatorId="de.common.DateValidator" />

</p:calendar> 

Then doing some validation on the given date:

@FacesValidator(DateValidator.VALIDATOR_ID)
public class DateValidator implements Validator {

    public static final String VALIDATOR_ID = "de.common.DateValidator";

    @Override
    public void validate(FacesContext facesContext, UIComponent component, 
        Object value) throws ValidatorException {

        Date inputDate = (Date) value;
        Calendar cal = Calendar.getInstance();
        cal.setTime(inputDate);
        if (cal.get(Calendar.YEAR) < 1000) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please provide a date with 4 digits for the year", null));
        }
    }

I know this prevents dates below 1000 but in my case it is absolutely clear that the date can not be lower then 2000.

So the suggestion is: Use a Validator to make sure the dates are correct. I know it is not the perfect solution but maybe a possible workaround.

Otherwise, try to ask for this on the primefaces forum.

2
votes

Try to use readonly="true" at such case you don't need to use any server side validators. This option will allow to the end user only to pick up the date from calendar panel.

0
votes

Your pattern and f:convertDateTime have different patterns?

It's likely not able to figure out what you want in your converter as entering in data as dd/mm/yyyy - then your converter tries to convert that to MM/yyyy.

The issue you describe is because leniency is set to true in the simple date formatter in side primfaces (its a default action). To force is you then use your convertDatetime which should fix it but your patterns may not match it seems.

However, if using a verison of PF < 4 you'll then get java script issues as there is a bug in p:calendar converters returning null objects after validation - you can do some manual fixes for this inside the PF code after rebuilding it.