selectCheckboxmenu not recognizing selected values

UI Components for JSF
Post Reply
mateusscheper
Posts: 6
Joined: 10 May 2020, 01:27

10 May 2020, 01:47

Hey!

I'm new to Primefaces and I'm currently trying to make a form to create a new user.

In this form, I have three selectCheckboxMenu: one for groups (id="grupos"), one for entities (id="entidades") and one for units (id="unidades").
The selectCheckboxMenu for groups and entities works OK, but the one for units always show my requiredMessage and I click the commandButton as if it's empty.

novo-usuario.xhtml:

Code: Select all

<h:form>
                <p:panelGrid columns="2">
                    <h:outputLabel for="nome" value="Nome completo"/>
                    <p:inputText id="nome"
                                 required="true"
                                 requiredMessage="Nome completo obrigatório"
                                 autocomplete="true"
                                 value="#{userBean.nome}"
                                 title="Nome completo do colaborador."/>
                    <h:outputLabel for="email" value="E-mail"/>
                    <p:inputText id="email"
                                 required="true"
                                 requiredMessage="E-mail obrigatório"
                                 autocomplete="true"
                                 value="#{userBean.email}"
                                 title="Endereço de e-mail que será gerado com base no nome completo e domínio de e-mail definido no cadastro da Entidade ou Unidade."/>
                    <h:outputLabel for="senha" value="Senha"/>
                    <div class="ui-inputgroup">
                        <p:inputText id="senha"
                                    required="true"
                                    requiredMessage="Senha obrigatória"
                                    value="#{userBean.senha}"
                                    title="Insira uma senha ou gere uma senha aleatória no botão ao lado."/>
                        <p:commandButton icon="fas fa-dice-five"
                                         action="#{userBean.gerarNovaSenha()}"
                                         update="senha"
                                         process="@this"
                                         title="Gerar nova senha aleatória."/>
                    </div>
                </p:panelGrid>
                <p:panelGrid columns="2">
                    <h:outputLabel for="grupos" value="Grupos"/>
                    <p:selectCheckboxMenu id="grupos"
                                          required="true"
                                          multiple="true"
                                          filter="true"
                                          filterMatchMode="contains"
                                          label="Selecione"
                                          requiredMessage="Selecione um ou mais grupos"
                                          value="#{userBean.gruposId}"
                                          title="Grupos em que o colaborador atuará.">
                        <f:selectItems value="#{userBean.gruposDisponiveis}"
                                       var="grupo"
                                       itemLabel="#{grupo.nome}"
                                       itemValue="#{grupo.id}"/>
                    </p:selectCheckboxMenu>
                </p:panelGrid>
                <p:panelGrid columns="2">
                    <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 update="unidades" listener="#{userBean.selecionarUnidades()}" />
                        <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>
                </p:panelGrid>
                <p:commandButton action="#{userBean.cadastrarNovoUsuario}"
                                 type="submit"
                                 value="Cadastrar"
                                 title="Ao clicar em Cadastrar, o sistema validará as informações inseridas acima e registrará o colaborador no sistema."
                                 process="@form">
                    <!--<f:ajax execute="@form" render="@form"/>-->
                </p:commandButton>
            </h:form>
UserBean.java:

Code: Select all

    private Long[] gruposId;

    private Long[] entidadesId;

    private Long[] unidadesId;

    private List<Entidade> entidadesDisponiveis;

    private List<Unidade> unidadesDisponiveis;

    private List<Grupo> gruposDisponiveis;

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

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


    public Long[] getGruposId() {
        return gruposId;
    }

    public void setGruposId(Long[] gruposId) {
        this.gruposId = gruposId;
    }

    public List<Grupo> getGruposDisponiveis() {
        return grupoRepository.getGrupos();
    }

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

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

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

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

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

    public void setUnidadesId(Long[] unidadesId) {
        this.unidadesId = unidadesId;
    }
When I submit:
Image

What am I doing wrong?

Ps: Sorry for my bad english.

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

10 May 2020, 16:03

I think you are missing a Converter for "Grupo" to be able to turn a selected item from a String in the UI to a "Grupo" object.

or see the example here: https://www.primefaces.org/showcase/ui/ ... Menu.xhtml and use List<SelectItem> instead of List<Grupo>
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

mateusscheper
Posts: 6
Joined: 10 May 2020, 01:27

12 May 2020, 06:33

Melloware wrote:
10 May 2020, 16:03
I think you are missing a Converter for "Grupo" to be able to turn a selected item from a String in the UI to a "Grupo" object.

or see the example here: https://www.primefaces.org/showcase/ui/ ... Menu.xhtml and use List<SelectItem> instead of List<Grupo>
Thank you for your reply.

I'm not sure that would be the answer, since I can persist Grupo and Entidade, but no Unidade. Unidade is the only one that's not working.
I removed it from the code to test the other two selectCheckboxMenu and they work properly.

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

12 May 2020, 13:10

Keep digging. There has to be something different or wrong about that particular one.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

mateusscheper
Posts: 6
Joined: 10 May 2020, 01:27

18 May 2020, 20:08

Melloware wrote:
12 May 2020, 13:10
Keep digging. There has to be something different or wrong about that particular one.
I noticed that setUnidadesId never gets called (it should when I select an item).
Currently, I set unidadesDisponiveis (it's a List<Unidade>) upon selecting an item in selectCheckboxMenu id="entidades". I noticed that, if instead of doing this, I set unidadesDisponiveis to be populated directly from db inside init() (like entidadesDisponiveis), I can select items normally.
Do you have any idea why is that?

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: Google [Bot] and 37 guests