Search code examples
validationjsf-2primefaces

PrimeFaces disable validation on cancel button


In a form, I have some inputText with two commandButton, one to accept and one to cancel.

How can I disable validation for the cancel button only?

<h:form id="detailsForm">
  <p:inputText id="editUsername" value="#{userController.editUser.usrUsername}" />
  <p:inputText id="editFirstName" value="#{userController.editUser.usrFirstName}" />
  <p:inputText id="editLastName" value="#{userController.editUser.usrLastName}" />
  <p:commandButton value="Accept" update=":detailsForm" actionListener="#{userController.onDetailsEditAccept}" />
  <p:commandButton value="Cancel" update=":detailsForm" actionListener="#{userController.onDetailsEditCancel}" />
</h:form>

I already tried inserting required="false" on fields but it didn't work. I also tried inserting <f:validateBean disabled="true" /> on fields and it didn't work.


Solution

  • Use the attribute immediate="true" in your cancel commandButton. This will skip the entire processing of the form, tough, by skipping the Apply Request Values, Process Validations and Update Model Values phases.

    <p:commandButton value="Cancel" update=":detailsForm" actionListener="#{userController.onDetailsEditCancel}" immediate="true"/>