PrimeFaces disable validation on cancel button

Paolo Dragone picture Paolo Dragone · Feb 19, 2013 · Viewed 26.6k times · Source

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.

Answer

Fritz picture Fritz · Feb 19, 2013

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"/>