0

I have a selectCheckboxMenu with multiple=true where I set a few Entidades. After that, it populates another selectCheckboxMenu (Unidades) based on what Entidades I selected (ManyToOne relation). The problem is, when I click submit, Primefaces says my selectCheckboxMenu for Unidades is empty.

.xhtml:

<h:form>
    <h:panelGrid>
        <h:outputLabel for="entidades" value="Entidades"/>
        <p:selectCheckboxMenu id="entidades"
                              required="true"
                              multiple="true"
                              filter="true"
                              filterMatchMode="contains"
                              label="Selecione"
                              requiredMessage="Selecione uma ou mais entidades"
                              value="#{userBean.entidadesId}"
                              title="Marque as entidades nas quais o colaborador faz parte.">
            <p:ajax listener="#{userBean.onEntidadesChange}" update="unidades"/>
            <f:selectItems value="#{userBean.entidadesDisponiveis}"
                           var="entidade"
                           itemLabel="#{entidade.nome}"
                           itemValue="#{entidade.id}"/>
        </p:selectCheckboxMenu>

        <h:outputLabel for="unidades" value="Unidades"/>
        <p:selectCheckboxMenu id="unidades"
                              required="true"
                              multiple="true"
                              filter="true"
                              filterMatchMode="contains"
                              label="Selecione"
                              requiredMessage="Selecione uma ou mais unidades"
                              value="#{userBean.unidadesId}"
                              title="Marque as unidades nas quais o colaborador faz parte.">
            <f:selectItems value="#{userBean.unidadesDisponiveis}"
                           var="unidade"
                           itemLabel="#{unidade.nome}"
                           itemValue="#{unidade.id}"/>
        </p:selectCheckboxMenu>
    </h:panelGrid>
</h:form>
<p:commandButton action="#{userBean.cadastrarNovoUsuario}"
                 value="Cadastrar"/>

UserBean.java:

@Named
@RequestScoped
public class UserBean implements Serializable {

    private Long[] entidadesId;

    private Long[] unidadesId;

    private List<Unidade> unidadesDisponiveis;

    private List<Entidade> entidadesDisponiveis;

    @PostConstruct
    public void init() {
        entidadesDisponiveis = entidadeRepository.getEntidades();
    }

    public void onEntidadesChange(){
        if (entidadesId != null && entidadesId.length > 0) {
            unidadesDisponiveis = unidadeRepository.getUnidadesByEntidades(entidadesId);
        } else {
            unidadesDisponiveis = new ArrayList<>();
        }
    }

    public List<Entidade> getEntidadesDisponiveis() {
        return entidadesDisponiveis;
    }
    public List<Unidade> getUnidadesDisponiveis() {
        return unidadesDisponiveis;
    }

    public Long[] getEntidadesId() {
        return entidadesId;
    }

    public void setEntidadesId(Long[] entidadesId) {
        this.entidadesId = entidadesId;
    }

    public Long[] getUnidadesId() {
        return unidadesId;
    }

    public void setUnidadesId(Long[] unidadesId) {
        this.unidadesId = unidadesId;
    }

}

I noticed that, if I change {#userBean.getUnidadesDisponiveis()} for #{#userBean.unidadesDisponiveis} and populate it inside an init() method, I can pass validation, but then I won't be able to populate it based on what I choose in my selectCheckboxMenu id="entidades".

What could I be doing wrong?

I'm using Primefaces 8.0 and I get my data from PostgreSQL via Hibernate/C3P0.

Mateus
  • 56
  • 1
  • 9
  • You have several bssic things and assumptions wrong. Check https://www.primefaces.org/showcase/ui/ajax/dropdown.xhtml works perferctly. I'll try to find duplicate which best suites your popblem – Kukeltje May 22 '20 at 05:35
  • One of your other errors http://stackoverflow.com/questions/2090033/ddg#2090062 – Kukeltje May 23 '20 at 08:02
  • And the duplicate AND the PF showcase contains all relvant info... Find the differences in what you do... – Kukeltje May 23 '20 at 08:04
  • 1
    We noticed that you created a new question. Over there you indeed fixed the problem of abusing the getter methods for business logic. But over there you did still not explicitly reconfirm that your bean is in the view scope. Not doing so confirms the "work around" of needing to populate the items in the postconstruct. The abovelinked duplicate also told you to put the bean in view scope. – BalusC May 23 '20 at 11:13
  • I edited this question and deleted the other one. SO said "If this question doesn’t resolve your question, ask a new one", so I did. Sorry about that. I was looking at the dropdown example and noticed that it populates the objects inside init(). I did that and it works, but I need to populate unidadesDisponiveis based on which Entidades I choose, so it doesn't work when inside init(). Doing like my code above populates it correctly, I can even see the itemLabel and itemValue in my html, but it fails when I click submit. Based on what I edited above, where could I go from there? – Mateus May 23 '20 at 23:49
  • 1
    Start reading the previous comment again... Especially the last sentence. And read the duplicate again, especially the first and second sentence. – Kukeltje May 23 '20 at 23:58
  • It worked. As I was reading, ViewScoped works only while I'm the given page, and RequestScoped works only for the given request, correct? Changing from RequestScoped to ViewScoped worked perfectly. Thank you for helping me. – Mateus May 24 '20 at 01:37

0 Answers0