Search code examples
validationjsffacelets

Execute commandButton inside form with validation errors


I want to execute a function in the backingbean, using a CommandButton within a form with validation errors.

Using the code example, I want to call function 'resiCon.cancel()' regardless of whether validation errors on the form or not.

Can you help me?

<h:form id="mainform">

    ....      
    <h:inputText value="#{resiCon.code">
        <f:validateLongRange minimum="1" />
    </h:inputText>

    <h:commandButton id="save" value="Save" update="mainform" action="#{resiCon.save()}"/>
    <h:commandButton id="cancel" value="Cancel" update="mainform" action="#{resiCon.cancel()}"/>

</h:form>

Thank You, very much.

Tomcat v7.0, Myfaces 2.1.5, Spring 3.1.0


Solution

  • There are at least 2 ways:

    1. Add immediate="true" to the cancel button. This skips all input fields which do not have this set.

      <h:commandButton id="cancel" ... immediate="true" />
      

      This may only not work when you're already using immediate="true" in some input fields to prioritize validation.

    2. Let it perform an ajax request wherein you only execute @this and thus ignore all fields.

      <h:commandButton id="cancel" ... >
          <f:ajax execute="@this" render="@all" />
      </h:commandButton>