Change validation behavior in Primefaces components

Asked

Viewed 1,333 times

6

I have 2 generic templates for crud. The commandButton Template search CRUD.XHTML (Code 2) is with the following logic: (When opening the page, all input fields are disabled, when you click the first time on the Search button, it enables all fields, if you click again without filling any field, it searches all records of my object, if any or all of the fields are filled in it fetches the records according to the data of the fields).

I have the following problem: To validate the data when saving, I am using the annotation **@NotNull**, then when I click the search button and do not report anything in my inputText description it does the validation. To circumvent this behavior, I made use of the process=@this in my commandButton search, but I got a new problem. Using process=@this in the component *commandButton* search, it is not considering the information filled in the component of inputText description of my screen, so when clicking the search button, is always searched for all the data. To solve this problem (With a great help from friend Rafael in another post [insert the link description here][1]), I left my commandButton with the *process=@this* and in my inputText description I used p:ajax event="change" process="@this" and is working properly.

But I noticed I’m gonna have to insert this Event ajax in all fields of all the screens that I will use this template, so I’m trying to use the process in my commandButton search of my tamplate CRUD.XHTML (Code 2) as follows process="@this pnlCampos" and it’s not working because it’s falling into validation.

If anyone knows how to get around this situation and can help.

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.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!

  • Have you tried using the process="@form"? So it processes all fields within the form of your template

  • Hello Rafael. I tried with "@form" alone inside the process, so as it processes everyone inside the form, it fell into validation. But I didn’t test it that way "@form pnlCampos"

1 answer

0

From what I understand, you need to run the validation only when you click on Salvar, and not when Pesquisar.

If this is really it, add the immediate="true" in his commandButton research.

  • I’ll take the test, thanks for the info response.

  • It didn’t work info.system. the field no longer fell in validation but when I inform a filter in inputText to search, the component is not being processed, so it is always searching for all records.

  • @Edegersil, I put this answer because I have a similar situation in my project. I read a little more about it, and I suggest to insert the immediate="true" inputs that need to process.

  • Thank you for the info.system. Again without success, I also inserted in my inputText the "immediate=true", but still no search when you insert a filter, always searching all the records. I did a reading on the internet and saw some people indicating the use of a Binding when you want to get the values of the component even if it is not being processed. But for the example I saw on the link http://moacirrf.com.br/blog/? p=89 (Case 2), I didn’t see how to use in my generic CRUD bean.

  • Forgetting the immediate, on your Search button, you have already tried to include the ID of the other fields in the process? process="@this input1 input2" separated by space...

  • Thank you for the info.system. I tried only this way "process=@this pnlCampos" Because pnlCampos is where I inform my input components, according to the CRUD.XHTML template that is along with my question. And it didn’t work.

  • info.sistemas, I did another test directly passing the id of inputText that I do not want q validate. And again unsuccessfully, it always falls into validation.

Show 2 more comments

Browser other questions tagged

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