I got a problem with p:selectOneMenu of PrimeFaces in JSF. I read a lot of questions asked about the same problem but nothing helped me.
When I set up my component the same way I used to do wherever in my project, if I try to select one of items of my selectOneMenu, this error appears :
Validation Error: Value is not valid
Lot of people resolve this by correcting the Converter class or equals() methods, but nothing look wrong in mines.
Converter
@RequestScoped
public class BaremeConverter implements Converter {
@EJB
private BaremeBean baremeBean;
@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if(value != null && value.trim().length() > 0) {
try {
return baremeBean.loadById(Integer.parseInt(value));
} catch(NumberFormatException e) {
return null;
}
}
else {
return null;
}
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object object) {
if(object != null) {
return String.valueOf(((Bareme) object).getId());
}
else {
return null;
}
}
}
BaremeBean is the entityBean of this class which is loading data fine. My workspace is full of converters like this so except if i miss something in this one, it should work here.
equals() method of class Bareme
@Override
public boolean equals(Object object) {
if (!(object instanceof Bareme)) {
return false;
}
Bareme other = (Bareme) object;
return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
}
This is the equals() method generates by Netbeans and nothing looks wrong here too.
Finally, I give you the code of the component I use, and same thing as previous ones, the same code works for others classes I got.
<h:outputLabel for="forfaitBareme" value="Barème" />
<p:selectOneMenu id="forfaitBareme" class="print-w100p-lab" value="#{transportFacturationBean.forfait.bareme}" converter="#{baremeConverter}" >
<f:selectItem itemLabel="" itemValue="#{null}" />
<f:selectItems value="#{transportFacturationBean.baremesForfait}" var="b" itemLabel="#{b.id}" itemValue="#{b}" />
<p:ajax event="change" update=":centralPanel" process="@form" />
</p:selectOneMenu>
transportFacturationBean.baremesForfait is a java.util.List which contains few Bareme.
You should know that the code below is working well using another custom object of my project. Camion is implemented the same way as Bareme, their converters are similar, and their equals() method are both the ones generated by Netbeans.
<h:outputLabel for="forfaitCamion" value="Camion" />
<p:selectOneMenu id="forfaitCamion" class="print-w100p-lab" value="#{transportFacturationBean.forfait.camion}" converter="#{camionConverter}" >
<f:selectItem itemLabel="" itemValue="#{null}" />
<f:selectItems value="#{transportFacturationBean.camions}" var="c" itemLabel="#{c.type}" itemValue="#{c}" />
<p:ajax event="change" update=":centralPanel" process="@form" />
</p:selectOneMenu>
Any help would be appreciated ! Thanks in advance !