How to bind a Date with calendar control in primefaces using binding attribute? - jsf

I want to bind a date with a calendar control after a specific value is selected from an autocomplete. But the following exception occurs :
javax.servlet.ServletException: java.util.Date cannot be cast to javax.faces.component.UIComponent
<p:autoComplete value="#{rechargeCustomerBean.school.schoolName}" completeMethod="#{rechargeCustomerBean.completeSchool}" required="true" />
<p:calendar mode="popup"
navigator="true" pattern="dd-MM-yyyy" effect="fadeIn"
showButtonPanel="true"
binding="#{rechargeCustomerBean.school.expiryDate}" />

Are you sure you want to bind?
Use the value attribute instead.
Also, add <p:ajax to your calendar to update the calendar and you are ready to go.
like this
<p:autoComplete value="#{rechargeCustomerBean.school.schoolName}" completeMethod="#{rechargeCustomerBean.completeSchool}" required="true">
<p:ajax event="itemSelect" update="idOfCalendar" />
</p:autoComplete>
Change
binding="#{rechargeCustomerBean.school.expiryDate}"
into
value="#{rechargeCustomerBean.school.expiryDate}"
so it will look like this
<p:calendar value="#{rechargeCustomerBean.school.expiryDate}" id="idOfCalendar"..... />

use of binding attribute in <p:calender> might cause the problem...try chaging it with value=#{...}
also make sure your bean's "expiryDate" is of type Util.Date
hope this could resolve your problem..for detailed explanation see here

Related

p:calendar value not set in backing bean

Hi I am using PrimeFaces. I have a <p:calendar> in a dialog box and I would type date manual . But my date is not setting in bean's variable. It works for dataSelect event but don't work keypress or keydown event. I am wondering it is also working outside dialog box. Here is my code:
<p:calendar readonlyInput="false" id="Date1" rendered="true"
navigator="true" showButtonPanel="true"
styleClass="form-control cal_input" showOn="both"
pattern="#{sessionManager.defaultDateFormat.substring(0,11)}"
yearRange="#{appLabel['year_Range']}" tabindex="203"
value="#{mangeCreateOrdersTO.searchOrderPatientBean.DOB}"
disabled="false" mask="true"
maxdate="#{mangeCreateOrdersTO.maxDateOfBirth}"
autocomplete="#{appLabel['autoCompleteSetting']}"
onkeypress="return disableEnterKey(event)">
<p:ajax event="dateSelect" process="#this" partialSubmit="true" ignoreAutoUpdate="true" update="form:Date1"/>
<p:ajax event="keydown" process="#this" immediate="true" partialSubmit="true" ignoreAutoUpdate="true" listener="#{mangeCreateOrdersTO.add(mangeCreateOrdersTO.searchOrderPatientBean.DOB)}"/>
</p:calendar>
Please help me to get rid from this problem. Thanks in advance.
I found what was wrong. So:
My <p:Calendar> must be under the <h:form> which I was missing.

ice:selectInputDate - valueChangeListener attribut not working when date entered manually

When I try to enter dates manually in tag that uses ice:selectInputDate, the valueChangeListener attribute doesnt work. When I click on the calendar symbol and select the date, the valueChangeListener works fine and my debug is redirected at proper method in bean.
Can anyone suggest on how to use valueChangeListener for this ice:selectInputDate when date is entered manually.
ValueChangeListener is not necessary
<ice:selectInputDate id="d1" renderMonthAsDropdown="true"
renderYearAsDropdown="true"
rendered="true"
renderAsPopup="true"
value="#{bean.myoDate}">
<f:convertDateTime pattern="dd/MM/yyyy"/>
</ice:selectInputDate>
make sure your panel/grid is enclosed with from
Try using ajax events as below
<ice:selectInputDate id="d1" renderMonthAsDropdown="true"
renderYearAsDropdown="true"
valueChangeListener ="#{yourbean.yourValueChangeListener}"
rendered="true"
renderAsPopup="true"
value="#{bean.myoDate}">
<f:convertDateTime pattern="dd/MM/yyyy"/>
<ace:ajax event="dateTextChange" execute="#this" />
<ace:ajax event="dateSelect" execute="#this" />
</ice:selectInputDate>

Primefaces focus form

<p:inputText value="#{bmiCalculatorBean.height}" update="#form" id="height">
<p:ajax update="#form" />
<f:convertNumber pattern="0" />
</p:inputText>
<p:inputText value="#{bmiCalculatorBean.weight}" update="#form" id="weight">
<p:ajax update="#form" />
<f:convertNumber pattern="0" />
</p:inputText>
I have a problem where after I enter a value into the first inputText and click on the "weight" inputText after a short delay the focus is lost and you need to click on the input box again.
How do I fix this?
This is caused by update="#form" and expected behavior.
In general, just do not update parts which do not need to be updated and update only those which really need to be updated. E.g. only the result calculated so far and the <h|p:message> of the current input:
<p:ajax update="calculatedResult currentInput_message" />
True, this may end up in a boilerplate of IDs in update attribute in large forms. The PrimeFaces Selectors (PFS, supporting jQuery selector syntax in JSF selectors) may then help a lot in this.

Check for validation errors in part of a form in JSF

I'm building a Seam application, which is basically a huge form divided into different parts, or modules. I need a way to figure out when a module is "complete", meaning all validation for the fields in that module passes. I then need to do something in the view, setting a css-class or whatever.
Something like:
<a:region id="region1">
<s:div styleClass="#{invalid ? 'errors' : ''}">
<h:inputText required="true" id="input1" />
<h:inputText required="true" id="input2" />
<h:commandButton value="Save this section" reRender="region1" />
</s:div>
</a:region>
I figured I had two options:
Using some sort of view-logic (like #{invalid} for a single field)
Using a method in the bean, where I get all components for the module programmatically, and check them for validation errors.
However, I can't find any way to do any of them. Any ideas if this is even possible?
We're using JSF 1.2 with Seam.
Thanks.
You can use UIInput#isValid() to check if a validation error has occurred on the particular input component.
<s:div styleClass="#{!input1.valid or !input2.valid ? 'errors' : ''}">
<h:inputText binding="#{input1}" required="true" id="input1" />
<h:inputText binding="#{input2}" required="true" id="input2" />
<h:commandButton value="Save this section" reRender="region1" />
</s:div>

Form value not passed to Seam bean after a4j reRender

I'm making a webapp in Seam but ran into a problem I can't seem to fix.
I have a JSF form where the customer can select a reservation type through a combobox. Based on the selected value, other form components gets rendered.
For example: the customer selects Hours as reservation type, a panelGroup gets rendered where the customer can select a start- and an end hour. But if the customer would select 'part of the day' as reservation type, a selectOneMenu gets rendered where the customer can select a part of the day (morning, afternoon, evening)
The rerendering well but the values of the components with a rendered conditional won't get passed to the bean. They stay null values.
This is the code i'm talking about:
<s:div id="spot"
rendered="#{selectedProduct.productType.name eq 'Flex Spot'}">
<h:panelGrid columns="2">
<h:outputText value="Reservation Type" />
<h:selectOneMenu value="#{selectedPeriodPart}">
<s:selectItems
value="#{productManager.getAvailableDayPartsSpot()}"
var="daypart"
label="#{daypart.label}"></s:selectItems>
<s:convertEnum />
<a4j:support ajaxSingle="true"
event="onchange"
action="#"
reRender="spot">
</a4j:support>
</h:selectOneMenu>
<h:outputText id="date_spot" value="Date" />
<a4j:outputPanel id="calendar_spot" layout="block">
<rich:calendar value="#{reservation.reservationPeriod.startDate}"
locale="en" cellWidth="24px"
cellHeight="22px"
style="width:200px" />
</a4j:outputPanel>
<h:outputText rendered="#{selectedPeriodPart eq 'DAY_PART'}"
value="Daypart" />
<h:selectOneMenu value="#{selectedDaypart}"
rendered="#{selectedPeriodPart eq 'DAY_PART'}">
<f:selectItem id="si_morning" itemLabel="Morning (6:00 - 12:00)"
itemValue="morning" />
<f:selectItem id="si_afternoon"
itemLabel="Afternoon (12:00 - 18:00)" itemValue="afternoon" />
<f:selectItem id="si_evening" itemLabel="Evening (18:00 - 00:00)"
itemValue="evening" />
</h:selectOneMenu>
<h:outputText rendered="#{selectedPeriodPart eq 'HOURS'}"
value="Hours" />
<h:panelGroup id="hours_spot"
rendered="#{selectedPeriodPart eq 'HOURS'}">
<ui:include src="/includes/reservation/select_hours.xhtml" />
</h:panelGroup>
</h:panelGrid>
</s:div>
Note: The calendar value do get passed back to the bean but the value of this piece of code doesn't (it does if you remove the rendered conditional):
selectOneMenu value="#{selectedDaypart}" rendered="#{selectedPeriodPart eq 'DAY_PART'}"
You need to ensure that the conditionals responsible for the outcome of the rendered attribute are also the same in the subsequent request of the form submit. Because when a component isn't rendered, JSF won't apply the request values them.
In a nutshell, the property behind #{selectedPeriodPart} needs to be the same in the subsequent request. There are several solutions:
Put bean in session scope. Easiest solution, but bad for server memory and client experience (updates would be reflected in multiple tabs/windows in same session).
Pass it through using <h:inputHidden>. Not sure though how this fits in the Ajax/Richfaces picture. Better use <a4j:keepAlive> here.
Use a conversation scope. Seam offers facilities for this.
I fixed it -.- Nothing was wrong with the code I posted.
Because I wasn't able to solve this issue I continued on an other page in the same conversation. I noticed some more strange behaviour: outjection of a variable didn't work etc.
I figured the mistake was in some other part of the code which, after corrected, fixed the whole problem.
Thx for answering guys!

Resources