Alex L.
Alex L.

Reputation: 61

Primefaces calendar does not set the date

I have yet another misunderstanding of Primefaces work logic.

For now <p:calendar> component cannot set selected date when I press submit button.
I see that it successfully enters into actionListener method, but date value is null there.

First of all I tried create my calendar using standard PF example. It looked too simple and I thought according this that the component should call value setter when the user select the date or submit the form. But it did it neither in the first nor in the second case. Well, I opened Google and found a few posts:

Primefaces Calendar Setting Date Value in Backing Bean
p:calendar value not set in backing bean

I ensured that my calendar is located between <h:form></h:form> tags. Also I tried to add process="@this" and process=":beginDateForm :endDateForm @this", where beginDateForm and endDateForm are forms contained <p:calendar> components.
Also I found the post and tried to create SelectEvent listener method:

private void changeDate(SelectEvent event) {
    beginDate = (Date) event.getObject();
}

But unsuccessfully.

I also tried to change Date using valueChangeListener:

<h:form id="beginDateForm">
    <p:calendar id="passBeginDate" valueChangeListener="#{territoryFormBean.changeDate}" mode="popup" readonly="true" pattern="dd.MM.yyyy" showOn="button" value="#{territoryFormBean.beginDate}" />
</h:form>

Of course I changed event to ValueChangeEvent.

After that I moved <p:calendar> and <p:commandButton> components into the same <h:form> and tried two different process values process="passTerrForm:passBeginDate passTerrForm:passEndDate @this" and process="@form @this" and process="@form" In the last case button does not trigger even the listener method.

My current components are:

<p:commandButton value="Search" id="searchPassButton" actionListener="#{territoryFormBean.search}" update=":passTerrForm:territoryTable" process="passTerrForm:passBeginDate passTerrForm:passEndDate @this" partialSubmit="true" />

<p:column>
    <p:calendar id="passBeginDate" mode="popup" readonly="true" pattern="dd.MM.yyyy" showOn="button" value="#{territoryFormBean.beginDate}" />
</p:column>
<p:column>
    <p:calendar id="passEndDate" mode="popup" readonly="true" pattern="dd.MM.yyyy" showOn="button" value="#{territoryFormBean.endDate}" />
</p:column>

Guys, could you please suggest anything else. Or probably you can see what is wrong in my code. I cannot understand why the component does not call setter.

Upvotes: 1

Views: 7682

Answers (3)

Alex L.
Alex L.

Reputation: 61

Well, guys, I found my mistake. Stupid mistake. Yet another re-read the PF documentation showed that using readonly parameter is incorrect for my goals. I wanted to prevent the manual date input directly into <p:calendar> text field. But according the documentation: readonly -

Flag indicating that this input element will prevent changes by the user

But I need readonlyInput parameter which

Makes input text of a popup calendar readonly.

So the second parameter prevents input, while the first one prevents changes totally.
Thank you for your help.

Upvotes: 4

Siddharth.Singh
Siddharth.Singh

Reputation: 76

JSF Code:

<p:calendar id="from" value="#{pageBean.event.startDate}"   pattern="MM/dd/yyyy hh:mm:ss a" timeZone="GMT-4"/>

<p:commandButton id="addButton" value="Save" actionListener="#{pageBean.addEvent}" update=":@form"/>

You can write an addEvent method in the Bean where you need to add these calendar event to an eventModel and save it. This helps you to set the date and you can retrieve it as and when you need.

Java Code:

private ScheduleModel eventModel;
private ScheduleEvent event = new DefaultScheduleEvent();
public String addEvent(ActionEvent actionEvent) {    

   if(event.getId() == null){
        eventModel.addEvent(event);
   }
}

Hope this helps!!

Upvotes: 0

imnotthatrobot
imnotthatrobot

Reputation: 147

Trying to make an analogy with a code that I have, I think that putting the button into the same form as the calendar, and omitting the 'process' and 'partialSubmit' should work:

    <h:form id="beginDateForm">
     <p:calendar id="passBeginDate" valueChangeListener="#{territoryFormBean.changeDate}" mode="popup" readonly="true" pattern="dd.MM.yyyy" showOn="button" value="#{territoryFormBean.beginDate}" />
     <p:commandButton value="Search" id="searchPassButton" actionListener="#{territoryFormBean.search}" update=":passTerrForm:territoryTable" />
    </h:form>

I hope it helps!

Upvotes: 0

Related Questions