10 Replies Latest reply on Nov 29, 2006 8:35 AM by dilator

    <si:selectItems> Value is not a valid option.

    rmemoria

      Hi,

      I'm using JBOSS 4.0.5GA, SEAM 1.1.0RC1 and SelectItems 1.1.1beta2.

      I have a page to edit users properties, and one of theses fields is the user's company (using si:selectItems). The list of companies show up perfectly, but when I click "Ok" to save the user, in te validation phase I get the following message:

      "companies": Value is not a valid option.


      Bellow is useredit.xhtml (part of it)

      <h:selectOneMenu id="company" value="#{user.company}" required="true">
      <si:selectItems value="#{companies}" var="co" label="#{co.name}" noSelectionLabel="-" />
      </h:selectOneMenu>
      <h:message for="company" styleClass="erro" />


      and userBean.java

      @Stateful
      @Scope(value=ScopeType.CONVERSATION)
      @Name("users")
      public class UsersBean implements Users {

      @DataModel
      private List<Users> userList;

      @EJB
      private UsersSrv userSrv;

      @DataModelSelection
      @Out(required=false)
      private User user;

      @In(create=true)
      private EntityManager em;

      @SuppressWarnings("unchecked")
      @Factory("companies")
      public List<Company> createCompanyList() {
      return em.createQuery("from Company").getResultList();
      }

      @SuppressWarnings("unchecked")
      @Factory("userList")
      public void createUserList() {
      userList = em.createQuery("from User u join fetch u.company").getResultList();
      }

      @Begin
      public String edit() {
      return "edit";
      }

      @Begin
      public String new() {
      user = new User();
      return "edit";
      }

      @End
      public String save() {
      if (user.getId() == null) {
      usuSrv.saveNewUser(user);
      }
      else em.persist(user);

      return "return";
      }

      @End
      public String cancel() {
      return "return";
      }

      @Remove @Destroy
      public void destroy() {
      }
      }


      and User.java (just the beggining)

      @Entity
      @Name("user")
      @SequenceGenerator(name="USER_SEQ")
      public class User implements java.io.Serializable {
      
       @Id @GeneratedValue(generator="USER_SEQ")
       private Integer id;
      
       @Column(nullable=false, length=20, unique=true)
       private String account;
      
       @Column(nullable=false, length=50)
       private String name;
      
       @ManyToOne
       @JoinColumn(name="COMPANY_ID")
       private Company company;
      


      Any tip?


        • 1. Re: <si:selectItems> Value is not a valid option.
          pmuir

          The EntityConverter reloads the Entity from the persistence context when submitting using a different instance of your EntityManager. As a result the objects aren't the same.

          To get round this you have to use 'id equality' - override the equals method on the Entity to return true if the ids are equal.

          • 2. Re: <si:selectItems> Value is not a valid option.
            rmemoria

            Thanks peter,

            Now it's working fine.

            • 3. Re: <si:selectItems> Value is not a valid option.
              fonseca

              Hi Peter,

              I understand that the equals in my Entity should be overridden as to prevent the default comparison of instances, but is id equality a requirement somehow? Currently, my equals does a value comparison of all fields in the Entity, save for the id, and I'm still having the same error (Value is not a valid option). I'd like to avoid comparing ids if possible. Thank you.

              • 4. Re: <si:selectItems> Value is not a valid option.

                I am also having a problem with this. I have it working for one entity, but I get a jsf validation error for another. Both equals methods were generated using Eclipse 3.2.

                I cannot find the difference between the 2 entities. I will try the equals method that only compares the ids.

                • 5. Re: <si:selectItems> Value is not a valid option.

                  I tried the following on the offending entity, but I still get a validation error.

                   @Override
                   public boolean equals(Object obj)
                   {
                   final Size other = (Size) obj;
                   if(other.getId()==id) return true;
                   return false;
                   }
                  


                  Here is how I am using the facelets tags

                   <h:selectManyListbox value="#{recordSearch.transactionTypes}" id="transaction_type_select_many">
                   <f:selectItems value="#{listTransactionType}" />
                   <si:convertEntity entityClass="pipetracker.model.state.TransactionType" />
                   </h:selectManyListbox>
                   <br/>
                  
                   <h:selectManyListbox value="#{recordSearch.sizes}" id="size_select_many">
                   <f:selectItems value="#{listSizes}" />
                   <si:convertEntity entityClass="pipetracker.model.inventory.pipe.specs.Size" />
                   </h:selectManyListbox>
                  


                  This is how I am loading the items. They are loaded properly, but when I select a size I get a validation error.

                   // Select Items
                   @SelectItems(label="label")
                   private List<Size> listSizes;
                   public List<Size> getListSizes() { return listSizes; }
                   public void setListSizes(List<Size> listSizes) { this.listSizes = listSizes; }
                  
                   @Factory("listSizes")
                   public void loadSizes() { listSizes = sizeDAO.getAllSizes(); }
                  


                  What could be different about these two entites? Any assistance would be greatly appreciated.

                  • 6. Re: <si:selectItems> Value is not a valid option.

                    I stepped into org.jboss.seam.selectitems.jsf.EntityConverter and found that the entities are in fact looked up correctly. If I select 3 items each lookup works fine, but I am still getting the jsf validation error.

                    I am using myfaces 1.1.4, JBoss 4.0.4.GA and Seam 1.0.1 GA.

                    Any advice?

                    • 7. Re: <si:selectItems> Value is not a valid option.
                      pmuir

                      fonseca,

                      No, id equality is not an absolute requirement.

                      Lets say you have a nationality dropdown; we'll call the objects looked up for the dropdown v1, and the the objects looked up by the entity converter v2.

                      In the dropdown you select British as your nationality (which is Britishv1); the EntityConverter does a lookup based on id and retrieves a new copy of the British object (Britishv2).

                      You need to make sure that Britishv1.equals(Britishv2) and must assume that they are looked up in different persistence contexts.

                      What this boils down to as a general rule is that you must compare on some unique property.

                      I'll take a fresh look at lifting this requirement.

                      Jason,

                      is the id a primitive or an object? If its an object remember (object1 == object2) != (object1.equals(object2)

                      The Validation error says 'value is not a valid option' is given by myfaces if it cannot find the selected object in the original list, so yes, by this point the entity converter has looked up the correct objects, it's just they aren't equal.

                      • 8. Re: <si:selectItems> Value is not a valid option.
                        evdelst

                        check for null in the equals()
                        I use an older version of the SelectItem.
                        When you have the 'noSelectionLabel' I think the equals is called with a null as well (always return false when the parameter to equals is null

                        • 9. Re: <si:selectItems> Value is not a valid option.
                          fonseca

                          Thank you Peter, I seem to be getting to the problem now. I have a few Lists in my entity, and it seems the problem lies within comparing them. When having a this.somelist.equals(o.somelist) in my equals, wherever the lists are equivalent or not, the result is always false, therefore generating a jsf validation error (value is not valid) during the rendering of the page after the submit when jsf compares the selected item with all items in the dropdown list. During runtime, somelist is of the type org.hibernate.collection.PersistentBag. This class does not implement the equals from the List interface, but the equals from Object.

                          I have removed the comparison of lists in my equals, and it now works fine.



                          • 10. Re: <si:selectItems> Value is not a valid option.
                            dilator

                            I guess it would be nice if JSF would let you implement your own equals via the converter, that way we could automatically provide equality on the @Id property.