Intro- I have 2 p:calendar components, calreq has required="true"
while calnotreq is optional.
Code- p:calendar xhtml code with ajax calls
<p:outputLabel value="Calendar Required" for="calreq" />
<p:calendar label="Calendar Required" id="calreq"
required="true"
inputStyle="width: 100px;" showOn="button"
value="#{myBean.calReqObj}" >
<p:ajax event="dateSelect" listener="#{myAction.calReqChange()}"/>
<p:ajax event="change" listener="#{myAction.calReqChange()}"/>
</p:calendar>
<p:outputLabel value="Calendar Not Required" for="calnotreq" />
<p:calendar label="Calendar Not Required" id="calnotreq"
inputStyle="width: 100px;" showOn="button"
value="#{myBean.calNotReqObj}" >
<p:ajax event="dateSelect" listener="#{myAction.calNotReqChange()}"/>
<p:ajax event="change" listener="#{myAction.calNotReqChange()}"/>
</p:calendar>
Listener implementations
//Invoked everytime **calreq** changes, except for null case
public void calReqChange() {
//myBeanObj
System.out.println("getCalReqObj from ajax event: " + myBeanObj.getCalReqObj() );
}
//Invoked everytime **calnotreq** changes, including for null case
public void calNotReqChange() {
//myBeanObj
System.out.println("getCalNotReqObj from ajax event: " + myBeanObj.getCalNotReqObj() );
}
Description- It's part of a big chunk of code & is wrapped properly inside form/panel/panelgrid
. Both the ajax calls calReqChange() & calNotReqChange()
are invoked when date value is changed, either from calendar or keyboard.
Problem- calreq's ajax listener is not invoked under one scenario. When the date field is emptied/nullified, the listeners are not called & correspondingly backing bean object is not updated to null. (But calnotreq listeners are invoked)
What's the reason & How to resolve this?
Note-
p:calendar
value changes, the server sidemyBean.calReqOb
should also be immediately updated (not wait for commandbutton submit). That's why I included the listeners. No logic inside listeners, but when listener is invoked, bean object setters are automatically called.p:selectBooleanCheckbox & p:calendar id="dependentCal"
. When Checkbox is enabled,dependentCal
should pick current value fromp:calendar id="calreq"
. That's why I added ajax listeners, as it updatesmyBean.calReqObj
immediately. To keep my post simple, I didn't add this piece of code.