Forums Register Login

Issue with jsf value change listener

+Pie Number of slices to send: Send
I am using jsf2.0 with primefaces. In my application there is one registration form ,in that i have city and state field.. when i am select the state it automatically retrieve the corresponding city on the city field, after selecting the city i am always getting the error message like 'City: Validation Error: Value is not valid';


here is my xhtml code..

<h:outputText value="State*"></h:outputText>

<p:selectOneMenu id="currentState"
value="#{volunteerBean.currentState}" required="true"
immediate="true" requiredMessage="Select your state" valueChangeListener="#{functions.valuechagemethod}"
label="State" style="width:196px">
<f:ajax execute="@this" render="currentCity"></f:ajax>
<f:selectItem itemLabel="select" itemValue=""></f:selectItem>
<f:selectItems value="#{volunteerBean.stateList}" var="item"
itemLabel="#{item}" itemValue="#{item}"></f:selectItems>
<p:ajax event="change" update="msg16" />
</p:selectOneMenu>
<h:message for="currentState" id="msg16" styleClass="alert" />
<p:tooltip for="currentState" value="Select Your State" style="color:blue;font-size:80%;" showEffect="fade" hideEffect="fade"/>

<h:outputText value="City*"></h:outputText>
<p:selectOneMenu id="currentCity"
value="#{volunteerBean.currentCity}" required="true"
immediate="true" requiredMessage="Select your city"
label="Country" style="width:196px">
<f:selectItem itemLabel="Select" />
<f:selectItems value="#{functions.retList}" var="item"
itemLabel="#{item}" itemValue="#{item}"></f:selectItems>
<p:ajax event="change" update="msg15" />
</p:selectOneMenu>
<h:message for="currentCity" id="msg15" styleClass="alert" />
<p:tooltip for="currentCity" value="Select Your City" style="color:blue;font-size:80%;" showEffect="fade" hideEffect="fade"/>




here is my bean method

public void valuechagemethod(ValueChangeEvent e)
{
String sval=e.getNewValue().toString();
Session session = null;
SessionFactory sessionFactory = HibernateUtil.getInstance();
session = sessionFactory.getCurrentSession();
Transaction tr= session.beginTransaction();
long sid=(Long) session.createQuery("select state_id from
StateBean where state_name='"+sval+"'").uniqueResult();
retList = session.createQuery("select city_name from CityPojo
where state_id='"+sid+"'").list();
setRetList(retList);
tr.commit();
}

public List<VolunteerBean> getRetList() {
return retList;
}

public void setRetList(List<VolunteerBean> retList) {
this.retList = retList;
}
public List<VolunteerBean> retList;



u8Vr0.png
screen shot
[Thumbnail for u8Vr0.png]
+Pie Number of slices to send: Send
Welcome to the JavaRanch, Sathez! You can make your code and XML more readable using this advice: https://coderanch.com/how-to/java/UseCodeTags

Selection controls will throw a validation error when the list of valid values (as defined by its selectItems) doesn't contain the selected value. This can happen, for example, when the backing bean was defined with Request scope (which is almost entirely useless in JSF).

You coded "immediate=true" on your control, however, and that can be a problem, since "immediate" doesn't mean "right now", it means "submit the form, but not the data values". Which means that your updated control value won't be sent to the server.
Hug your destiny! And hug this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3765 times.
Similar Threads
selectOneMenu call a method on select item
Getting One Dropdown Menu Selection to Affect the Values of a Second Dropdown Menu
selectItems does not fire ajax listener
Updating a form in primefaces when a selection is made in the datatable
multiple form in xhtml page nad update different controlls on two different forms
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:52:33.