Conditional validation in Primefaces component

Asked

Viewed 1,333 times

3

Hello!

I have the following problem. I have inputText description which gets disabled by default. When I click the search button, it enables inputText and then instance an object for search. If I do not pass a filter in the description inputText, it searches all the records of the object I am using, if I pass a filter it brings the records according to the entered filter. In my class I have a @Notnull annotation in the description property, to treat the validation in the registration screen, but to prevent inputText to be validated when searching without any filter, I am using in my commandButton the process="@this"but if I pass a filter, this filter is not being "set" in the description property of my object and is always searching all records.

How do I handle this situation? If I remove the annotation from my property the logic works correctly, but if I use it I get this problem.

I am using the Bean Validations annotations to not have to do a validation inside my bean creating a series of if and Else, with Bean Validations I believe I will be treating the validations in the correct phase of the life cycle.

Page codes!

CRUD basic.xhtml template (Code 1)

<!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:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
<h:head>
  <title>${titulo}</title>
  <h:outputStylesheet name="core.js" target="head" library="js" />
  <h:outputStylesheet name="style.css" target="head" library="css" />
</h:head>

<h:body>

  <div id="dvStatus" align="center">
    <p:ajaxStatus onstart="PF('statusDialog').show();" onsuccess="PF('statusDialog').hide();" />
    <p:dialog modal="true" widgetVar="statusDialog" draggable="false" closable="false" resizable="false" showHeader="false">
      <p:graphicImage value="/resources/img/icon/ajaxloading3.gif" />
    </p:dialog>
  </div>

  <h:form id="formCrud">
    <ui:insert name="cabecalho">
      <ui:include src="tmenu.xhtml" />
    </ui:insert>

    <p:toolbar/>

    <ui:insert name="toolbar">
      <p:toolbar id="tool">
        <p:toolbarGroup>
          <ui:insert name="toolbarButtons" />
        </p:toolbarGroup>
      </p:toolbar>
    </ui:insert>

    <ui:insert name="panelExterno">
      <ui:insert name="panelCentral" />
      <center>
        <h:messages id="growl" showDetail="true" autoUpdate="true" closable="true" showSummary="false" errorClass="converterMsgError" warnClass="converterMsgWarn" infoClass="converterMsgInfo" fatalClass="converterMsgError" />
      </center>
    </ui:insert>

    <p:confirmDialog global="true">
      <p:commandButton value="Sim" type="button" styleClass="ui-confirmdialog-yes" icon="confirm" />
      <p:commandButton value="Não" type="button" styleClass="ui-confirmdialog-no" icon="cancel" />
    </p:confirmDialog>
  </h:form>
</h:body>

</html>

CRUD crud.xhtml template (Code 2)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui" template="/templates/basic.xhtml">

  <ui:define name="toolbarButtons">
    <ui:insert name="btnNew">
      <p:commandButton id="new" value="Novo" actionListener="#{backingBean.novo}" update="tool pnlCampos growl" disabled="#{backingBean.disableBtnNew}" icon="novo" style="width:100px; margin-left: 6px;" />
    </ui:insert>
    <ui:insert name="btnSave">
      <p:commandButton id="save" value="Salvar" actionListener="#{backingBean.salvar}" update="tool pnlCampos pnlTable growl" disabled="#{backingBean.disableBtnSave}" icon="gravar" style="width:100px;margin-left: 6px;" />
    </ui:insert>
    <ui:insert name="btnCancel">
      <p:commandButton id="cancel" value="Cancelar" actionListener="#{backingBean.cancelar}" process="@this" update="tool pnlCampos pnlTable growl" disabled="#{backingBean.disableBtnCancel}" icon="cancel" style="width:100px;margin-left: 6px;" />
    </ui:insert>
    <ui:insert name="btnFind">
      <p:commandButton id="search" value="Pesquisar" actionListener="#{backingBean.pesquisar}" process="@this pnlCampos" update="tool pnlCampos pnlTable growl" disabled="#{backingBean.disableBtnSearch}" icon="search" style="width:100px;margin-left: 6px;" />
    </ui:insert>
    <ui:insert name="btnExport" />
    <ui:insert name="toolbarExtraButtons" />
  </ui:define>

  <ui:define name="panelCentral">
    <ui:insert name="panelDeCadastro">
      <p:panel id="pnlCampos" header="${toolTitulo}">
        <ui:insert name="panelCadastro" />
      </p:panel>
    </ui:insert>
    <ui:insert name="table">
      <p:outputPanel id="pnlTable" deferred="true">
        <p:dataTable id="table" value="#{backingBean.model}" var="obj" rows="8" paginator="true" paginatorPosition="bottom" emptyMessage="" rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'even-row' : 'odd-row'}" rowIndexVar="rowIx" scrollable="true">
          <f:facet name="header">
            Registro(s) Cadastrado(s)
          </f:facet>
          <ui:insert name="colunasTabela" />

          <p:column id="edit" style="width: 10px; text-align:center;">
            <p:commandLink title="Editar Registro" actionListener="#{backingBean.editar}" process="@this" update=":formCrud:pnlCampos :formCrud:tool :formCrud:pnlTable :formCrud:growl">
              <p:graphicImage value="/resources/img/icon/editar_16x16.png" />
            </p:commandLink>
          </p:column>
          <p:column id="del" style="width: 10px; text-align:center;">
            <p:commandLink title="Deletar Registro" actionListener="#{backingBean.excluir}" process="@this" update=":formCrud:pnlCampos :formCrud:tool :formCrud:pnlTable :formCrud:growl">
              <p:graphicImage value="/resources/img/icon/excluir_16x16.png" />
              <f:setPropertyActionListener target="#{backingBean.rowData}" value="#{obj}" />
              <p:confirm header="Atenção" message="Você confirma a exclusão desse registro ?" icon="ui-icon-alert" />
            </p:commandLink>
          </p:column>
        </p:dataTable>
      </p:outputPanel>
    </ui:insert>
  </ui:define>
</ui:composition>

Position Registration Screen (Code 3)

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui">
<ui:composition template="/templates/crud.xhtml">

  <ui:param name="titulo" value="Tela de Cargo" />
  <ui:param name="toolTitulo" value="Cadastro e Edição de Cargo" />
  <ui:param name="backingBean" value="#{cargoBean}" />

  <ui:define name="panelCadastro">
    <h:panelGrid id="pnlGrdCargo" columns="1" width="100%">
      <h:outputText value="Descrição:" />

      <p:inputText value="#{cargoBean.domain.descricao}" onkeyup="this.value = this.value.toUpperCase()" disabled="#{cargoBean.disabledOnDefault}" style="width: 30%;" />
    </h:panelGrid>
  </ui:define>

  <ui:define name="colunasTabela">
    <p:column headerText="Código" style="width: 50px;">
      <h:outputText value="#{obj.codigoCargoFuncionario}" />
    </p:column>
    <p:column headerText="Descrição">
      <h:outputText value="#{obj.descricao}" />
    </p:column>
  </ui:define>

</ui:composition>

</html>

Thank you!

1 answer

3


Your search button is not processing the description field.

You have two options to solve.

first: Process inputText by its button

<p:commandButton id="search" value="Pesquisar" actionListener="#{backingBean.pesquisar}" process="@this ID_DO_INPUT" update="tool pnlCampos pnlTable growl" disabled="#{backingBean.disableBtnSearch}" icon="search" style="width:100px;margin-left: 6px;" />

2nd: When you change the description, have him process

<p:inputText value="#{cargoBean.domain.descricao}" onkeyup="this.value = this.value.toUpperCase()" disabled="#{cargoBean.disabledOnDefault}" style="width: 30%;" >
        <p:ajax event="change" process="@this"/>
</p:inputText>
  • Thanks for the reply Rafael. I will test the two options and put the results.

  • Rafael thanks for the help, I tested and worked properly the two options, but I’m using the second because I have a generic layer of CRUD that is used on multiple screens, so the second option was more viable. Thank you.

  • You’re welcome, anything at all.

  • Rafael is fine. As I informed the two forms you mentioned works correctly, but using the 2 form "which is the one I’m wearing", I have verified that I will have to perform this partial processing with @this on all inputText of all sign up screens where my generic CRUD template is used. So I’m trying to use the first way you said it, but it’s not working, it’s always falling into the field validation. I edited my question by adding my full generic CRUD screens and the main registration screen I’m using.

  • I removed from my inputText job registration screen description (Code 3) the 2 option that Rafael reported and used the 1 option in my commandButton Search (Code 2) from the CRUD.xhtml template, passing @this and the panel id that is inside the ui:defines panelCentral and ui:Insert panelDedasting containing the registration inputText. But it didn’t work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.