1

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 !

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
robinvrd
  • 1,760
  • 12
  • 28
  • The duplicate http://stackoverflow.com/q/9069379 describes a third cause which you left unmentioned. – BalusC Apr 07 '17 at 12:54
  • @BalusC If you're talking about the selected item could be not in the available ones, my item is firstly null and as you can see i added the f:selectItem with value null which work for my Camion object. I also tried to select another item in my list of available ones in my ManagedBean but the error is still here. – robinvrd Apr 07 '17 at 13:34
  • Uh, I wasn't talking about null items. Just that the item which was selected is not anymore in list of available items at the moment when the form submit is actually processed. – BalusC Apr 07 '17 at 13:39
  • Sure, maybe I'm expressing myself badly but i checked this. I display my list of available items in the same form as my component and this list still contains each element I am able to select in my oneMenu when the error fires (knowing that my change event update this display). – robinvrd Apr 07 '17 at 13:45
  • "BaremeBean is the entityBean" - no, looks like BaremeBean is EJB, and Bareme could be a JPA entity bean. In which case its equals method is wrong, because it refers to the `other.id` field directly, so it risks seeing an empty proxy field. It should instead go through a getter: `other.getId()`. – Vsevolod Golovanov Apr 08 '17 at 11:58
  • A wrong equals method could be the root of the problem, since selectOne checks that one of the avalaible options was in fact selected. – Vsevolod Golovanov Apr 08 '17 at 12:03
  • My equals method is never call and this one is working fine when i check it manually. BaremeBean is my entityBean while Bareme is my pure Entity. equals don't look to be the problem here. – robinvrd Apr 10 '17 at 07:13
  • In Stackoverflow it is not needed to edit titles when things are solved. Accepted answers are the indication of things being solved – Kukeltje Apr 10 '17 at 09:26

1 Answers1

1

SOLVED ! The biggest smallest mistake I could imagine !

return baremeBean.loadById(Integer.parseInt(value));

My loadById method were returning a list instead of a simple object.... Sorry guys !

robinvrd
  • 1,760
  • 12
  • 28