0

I have a simple form, with some input and a selectonemenu:

            <h:panelGroup id="Client">
            <h:form id="formClient">
                <p:panelGrid columns="3" layout="grid"
                    columnClasses="labelWidth, ui-grid-col-3">
                    <p:outputLabel value="CIN:" for="txtCin" />
                    <h:panelGrid columns="2">
                        <p:inputText value="#{clientBean.client.identifiant}" id="txtCin"></p:inputText>
                        <p:commandButton value="Search" process="@parent"
                            styleClass="orangeButton" update="formClient"
                            style="margin-top: -10px;" action="#{clientBean.rechercher()}"></p:commandButton>
                    </h:panelGrid>
                    <p:message for="txtCin" />

                    <p:outputLabel value="Nom:" for="txtNom" />
                    <p:inputText id="txtNom" styleClass="form-control"
                        value="#{clientBean.client.nomClient}" required="true"
                        requiredMessage="Le nom est obligatoire" />
                    <p:message for="txtNom" />

                    <p:outputLabel value="Type de voie:" for="txtVoie" />
                    <p:selectOneMenu styleClass="form-control" 
                        value="#{clientBean.client.typeVoie}" id="txtVoie">
                        <f:selectItem itemLabel="Selectionnez voie..." itemValue=""  />
                        <f:selectItems value="#{clientBean.typeVoies}" var="typeVoie" itemLabel="#{typeVoie.libelle}" itemValue="#{typeVoie}" />
                    </p:selectOneMenu>
                    <p:message for="txtVoie" />

                    <p:outputPanel></p:outputPanel>
                    <p:outputPanel>
                        <p:commandButton value="Annuler" update="formClient"
                            styleClass="redButton" process="@this"
                            actionListener="#{clientBean.cancel()}" />
                        <p:commandButton ajax="false" value="Suivant"
                            styleClass="greenButton" action="Devis?faces-redirect=true"></p:commandButton>
                    </p:outputPanel>
                </p:panelGrid>
            </h:form>
        </h:panelGroup>

As you see here i have some fields that are a required, and a button to cancel (empties) the form:

@ManagedBean(name = "clientBean")
@SessionScoped
public class ClientBean implements Serializable {

    @EJB
    private IClientDAO clientDAO;
    @EJB
    private ITypeVoieDAO typeVoieDAO; 

    private List<TypeVoie> typeVoies;

    private static final long serialVersionUID = -3324864097586374969L;

    private Client client = new Client();

    @PostConstruct
    public void init(){
        typeVoies = typeVoieDAO.findAll(); 
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public void rechercher(){
        if(client != null && client.getIdentifiant() != null){
            System.out.println(client.getIdentifiant());
            client = clientDAO.findByIdentifiant(client.getIdentifiant());
            if(client == null){
                cancel();
            }
        }
    }

    public void cancel(){
        client = new Client();
        System.out.println(typeVoies);
    }

    public String navigateToClientPage(){
        rechercher(); 
        return "client?faces-redirect=true";
    }

    public List<TypeVoie> getTypeVoies() {
        return typeVoies;
    }

    public void setTypeVoies(List<TypeVoie> typeVoies) {
        this.typeVoies = typeVoies;
    }
}

At the page loads, the selectonemenu loads perfectly, but when i click cancel, the selectonemenu gets emptied, even when i "search" for a client, and updates the form, all the other fields are filled properly, but the selectonemenu gets completely empty.

Using JSF 2.1 and primefaces 6.0

UPDATE: Added a converter, but nothing change ...

Ouerghi Yassine
  • 1,835
  • 7
  • 43
  • 72
  • Of what type are your `typeVoies` list elements. This could be a [missing converter](http://stackoverflow.com/questions/4734580/conversion-error-setting-value-for-null-converter) for complex objects problem. – irieill Aug 11 '16 at 12:24
  • Yep it is an object with multiple field, should i try and create a converter for ir? – Ouerghi Yassine Aug 11 '16 at 12:26
  • I suggest to do so. And its always a good option to add a `` to your page if you encouter problems to see if there are JSF errors. Nevertheless what do you exactly mean by _"selectOneMenu gets completely empty"_? – irieill Aug 11 '16 at 12:40
  • but if it is a converter issue, how does it even load the first time, and by gets empty, as u can see no values in the list ... i added the growl, no message is displayed – Ouerghi Yassine Aug 11 '16 at 13:02
  • It will load the first time, because the standard converter uses _Object.toString()_ to encode the complex object on the client side. Errors appear first, if you process the input on the server and try to convert the encoded object back. Can you concrete where we should _"see"_ that there are _"no values in the list"_ please. – irieill Aug 11 '16 at 14:54
  • @irieill when the page loads, you can actually see items in the dropdown menu, when i click on "cancel" there is no more items in the dropdown menu. – Ouerghi Yassine Aug 11 '16 at 15:18
  • Not even the option rendered by ``? – irieill Aug 11 '16 at 15:34
  • @irieill not even that – Ouerghi Yassine Aug 11 '16 at 15:36

1 Answers1

0

Make sure you dont empty typeVoies list at some point on your backingbean. And if you can share your full java class, you might get more accurate help.

  • i made sure that the list is not empty, i updated my question with the whole class – Ouerghi Yassine Aug 11 '16 at 13:00
  • yep, if i dont, the action wont work, because i have some input set to required. – Ouerghi Yassine Aug 11 '16 at 13:41
  • @MehmetBaşal That is a way to prevent other fields to be processed on the server side. With a 'cancel' you are not interested in e.g. validation errors to prevent the form to be submitted. So with this attribute you can state what should be processed, in this case only the button and skip all other fields: http://stackoverflow.com/questions/25339056/understanding-process-and-update-attributes-of-primefaces – Kukeltje Aug 11 '16 at 13:43