0

I know this is a common error, and that there is a lots of questions about it, i tired to implement from many places , i will introduce the major error.

Screenshot Here i want to select a client & a product from selectOneMenu with and insert them to another Table with other inputs as you can see here the client and the products are selected but when i try to add it returns as exeption Value is required ! i tried to solve the problem with this :

Validation Error: Value is not valid

Here is my jsf page:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:f="http://xmlns.jcp.org/jsf/core">
        <h:head>
            <title>Facelet Title</title>
        </h:head>
        <h:body>
            <f:view>


                <h:form>
                    <h1><h:outputText value="Create/Edit"/></h1>
                    <h:panelGrid columns="2">
                        <h:outputLabel value="Client:" for="client" />
                        <h:selectOneMenu id="client" value="#{recBean.rec.client}" converter="#{clientConverter}" title="Client" required="true" >
                            <!-- TODO: fixed -->

                            <f:selectItems value="#{CliBean.lister()}" var="C" itemLabel="#{C.nomCli}" itemValue="#{C}"  />
                        </h:selectOneMenu>
                        <h:outputLabel value="Produit:" for="produit" />
                        <h:selectOneMenu id="produit" value="#{recBean.rec.produit}" converter="#{produitConverter}" title="Produit" required="true" >
                            <!-- TODO: fixed -->
                            <f:selectItems value="#{proBean.lister()}" var="P" itemLabel="#{P.libProd}" itemValue="#{P}"/>
                        </h:selectOneMenu>
                        <h:outputLabel value="DateCreRec:" for="dateCreRec" />
                        <h:inputText id="dateCreRec" value="#{recBean.rec.dateCreRec}" title="DateCreRec" required="true" requiredMessage="The DateCreRec field is required.">
                            <f:convertDateTime pattern="MM/dd/yyyy" />
                        </h:inputText>
                        <h:outputLabel value="TypeRec:" for="typeRec" />
                        <h:inputText id="typeRec" value="#{recBean.rec.typeRec}" title="TypeRec" required="true" requiredMessage="The TypeRec field is required."/>
                        <h:outputLabel value="DescCli:" for="descCli" />
                        <h:inputText id="descCli" value="#{recBean.rec.descCli}" title="DescCli" required="true" requiredMessage="The DescCli field is required."/>
                        <h:outputLabel value="Situation:" for="situation" />
                        <h:inputText id="situation" value="#{recBean.rec.situation}" title="Situation" required="true" requiredMessage="The Situation field is required."/>
                        <h:outputLabel value="PersEstim:" for="persEstim" />
                        <h:inputText id="persEstim" value="#{recBean.rec.persEstim}" title="PersEstim" required="true" requiredMessage="The PersEstim field is required."/>
                        <h:outputLabel value="Estimation:" for="estimation" />
                        <h:inputText id="estimation" value="#{recBean.rec.estimation}" title="Estimation" required="true" requiredMessage="The Estimation field is required."/>
                        <h:outputLabel value="DegUrgence:" for="degUrgence" />
                        <h:inputText id="degUrgence" value="#{recBean.rec.degUrgence}" title="DegUrgence" required="true" requiredMessage="The DegUrgence field is required."/>
                        <h:outputLabel value="DescExpert:" for="descExpert" />
                        <h:inputText id="descExpert" value="#{recBean.rec.descExpert}" title="DescExpert" required="true" requiredMessage="The DescExpert field is required."/>
                        <h:outputLabel value="DateDebut:" for="dateDebut" />
                        <h:inputText id="dateDebut" value="#{recBean.rec.dateDebut}" title="DateDebut" required="true" requiredMessage="The DateDebut field is required.">
                            <f:convertDateTime pattern="MM/dd/yyyy" />
                        </h:inputText>
                        <h:outputLabel value="DateFin:" for="dateFin" />
                        <h:inputText id="dateFin" value="#{recBean.rec.dateFin}" title="DateFin" required="true" requiredMessage="The DateFin field is required.">
                            <f:convertDateTime pattern="MM/dd/yyyy" />
                        </h:inputText>

                        <h:commandButton value="Add" action="#{recBean.ajouter()}"/>
                    </h:panelGrid>
                </h:form>
            </f:view>

        </h:body>
    </html>

Here is managed bean:

@ManagedBean(name="CliBean")
@SessionScoped
public class ClientBean {
   ClientDAO dao = new ClientDAOImpl();
   public Client cli = new Client();


    private String logcli;



    public Client getCli() {
        return cli;
    }

    public void setCli(Client cli) {
        this.cli = cli;
    }

    public List<Client> lister(){
        return dao.findAll();
    }

 @Override
public String toString() {
    return String.valueOf(cli.getIdCli());
}

    public String LoginCheck(){

     Session ses = HibernateUtil.getSession();
     SessionFactory fac = HibernateUtil.getSessionFactory();
     ses.getTransaction();


             List<Client> list = ses.createSQLQuery("select * from client where login_cli='" + cli.getLoginCli() + "' and pwd_cli='" + cli.getPwdCli() + "'").list();
             if (list.size() > 0) {
                 //servlet session part

                 logcli = cli.getLoginCli();

                 HttpSession hs = sessionUtil.getSession();
                 hs.setAttribute("logcli",logcli);
                 //servlet session part
                 //BootFaces
                 FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Congratulations! You've successfully logged in.");
                 FacesContext.getCurrentInstance().addMessage("loginForm:password", msg);
                 //BootFaces

            return "/success.xhtml?faces-redirect=true";
        } else {
                //FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "client not found", ""));

                //BootFaces
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "", "client not found !");
                FacesContext.getCurrentInstance().addMessage("loginForm:password", msg);
                //BootFaces

               }
        try {
            ses.getTransaction().commit();
        } catch (Exception e) {
        }
        ses.close();
        return null;
        }
    public String afflogin(){
        return logcli;
         }

    public String doLogout(){
        HttpSession hs = sessionUtil.getSession();
        hs.invalidate();
        return "/index.xhtml";
         }



                        }

And finally converter:

@ManagedBean
@RequestScoped
@FacesConverter(forClass=Client.class)
public class ClientConverter implements Converter{


    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String codeString) {
    if (codeString == null && codeString.trim().length() > 0) {
        Integer code = Integer.valueOf(codeString);
        ClientServicesImpl clientServicesImpl = new ClientServicesImpl();
        return clientServicesImpl.searchForCode(code);
    }

return null;
}

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object clientObject) {
    if (clientObject == null) {
        return "";
    }

    if (clientObject instanceof Client) {
        Client client = (Client) clientObject;

        return client.getIdCli().toString();
        //return client.getIdCli() != null ? String.valueOf(client.getIdCli()) : null;

        //return String.valueOf(((Client) clientObject).getIdCli());
    } else {
        throw new ConverterException(new FacesMessage(clientObject + " is not a valid Client"));
    }
    }
}

Note that : equals()& hashCode() have been generated for entities client and product .

i think the major problem is from the converter . also i saw almost all the posts about selectOneMenu before i post my question. i wasted 3 days for this i dont want to waste more time

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Next time when you post a question: don't use all bold text. Post links to what you tried (See [ask]) and try to create an [mcve] as stated in the previous link. Just complaining does not help. – Kukeltje Apr 03 '18 at 21:11
  • 1
    this is my first post , i'm just a beginner i will make sure to keep that in mind , can i get some help now ? the duplicate mark i have already tried it ... – Rev Damage Apr 04 '18 at 00:00
  • If it is not a duplicate, start improving **this** question into a [mcve] – Kukeltje Apr 04 '18 at 05:23
  • Great you keep editing your question on the micro level, but if you do not update it to become a [mcve], I doubt you'll get any help on a question marked as a duplicate by BalusC. And your question is not java, java-ee or hibernate related. Pure JSF... – Kukeltje Apr 04 '18 at 15:10

0 Answers0