9 Replies Latest reply on Mar 26, 2009 12:12 PM by alan79

    Cancel Button - How to bypass form validation for required f

    alan79

      Hello Forum

      I would like to have a cancel Button on a form which returns the user to the prior form.
      I have some fields on the form with attribute required="true".
      These fields report correctly "Validation Error: Value is required." when clicking on cancel.

      Is there a way to bypass this validation?

      This the button:

      <h:commandButton id="cancelModify"
       value="#{cont.buttonCancel }"
       action="#{projectHandler.cancelModify}" />
      


      Thank you for a hint!
      Alan

        • 1. Re: Cancel Button - How to bypass form validation for requir
          nbelaevski

          Hello,

          Use "immediate" attribute. Refer to JSF documentation/books.

          • 2. Re: Cancel Button - How to bypass form validation for requir
            alan79

            Hi Nick

            Thank you! This works perfectly for the cancel button!
            But I have the same problem with rich:suggestionBox. Is there a way to make the suggestionBox work with the immediate attribute?

            Here is an example:
            The suggestionBox "work package" is followed by a required field "start date". As soon as I select a value from the suggestionBox the required field runs into the validation error. I tried to set the immediate attribute on the suggestionBox but it has no effect.

            <h:inputText style="margin:0px;"
            value="#{timerepHandler.reportCreatePackage}"
            disabled="false" id="workPackageInput" />
            <h:graphicImage
            value="#{facesContext.externalContext.requestContextPath}/../images/arrow.png"
            onclick="#{rich:component('suggestionWorkPackages')}.callSuggestion(true)"
            alt="" />
            </h:panelGrid>
            <rich:message for="workPackageInput">
            <f:facet name="passedMarker">
            <h:graphicImage styleClass="formImgageValidation"
            value="#{facesContext.externalContext.requestContextPath}/../images/passed.gif" />
            </f:facet>
            <f:facet name="errorMarker">
            <h:graphicImage styleClass="formImageValidation"
            value="#{facesContext.externalContext.requestContextPath}/../images/error.gif" />
            </f:facet>
            </rich:message>
            <h:outputText id="workPackagesSuggested"
            style="font-weight:bold" />
            </h:panelGrid>
            
            <rich:suggestionbox height="200" width="400"
            usingSuggestObjects="false" nothingLabel="No packages found"
            onobjectchange="printObjectsSelected(#{rich:element('workPackagesSuggested')},#{rich:component('suggestionWorkPackages')});"
            suggestionAction="#{timerepHandler.workPackageAutocomplete}"
            var="resp" for="workPackageInput"
            fetchValue="#{resp.packageName}"
            id="suggestionWorkPackages" tokens="," immediate="true">
            <h:column>
            <h:graphicImage value="../.././images/icon_wpackage.jpg" />
            </h:column>
            <h:column>
            <h:outputText value="#{resp.packageReference}" />
            </h:column>
            <h:column>
            <h:outputText value="#{resp.packageName}" />
            </h:column>
            
            <a4j:support event="onselect">
            
            <f:setPropertyActionListener value="#{resp}"
            target="#{timerepHandler.createReport.workpackages}" />
            
            </a4j:support>
            </rich:suggestionbox>
            </h:panelGrid>
            <h:outputLabel value=""/>
            
            <h:outputLabel for="start" value="#{cont.lblReportStart }" />
            <rich:calendar id="start"
            value="#{timerepHandler.createReport.start}" required="true"
            locale="de" popup="true" datePattern="dd.MM.yyyy HH:mm"
            showApplyButton="true" />
            <rich:message for="start">
            <f:facet name="passedMarker">
            <h:graphicImage styleClass="formImgageValidation"
            value="#{facesContext.externalContext.requestContextPath}/../images/passed.gif" />
            </f:facet>
            <f:facet name="errorMarker">
            <h:graphicImage styleClass="formImageValidation"
            value="#{facesContext.externalContext.requestContextPath}/../images/error.gif" />
            </f:facet>
            </rich:message>
            


            Thank you very much for another hint!
            Best regards
            Alan


            • 3. Re: Cancel Button - How to bypass form validation for requir
              ilya_shaikovsky

              add immediate to your support. In general request from support rises validation because suggestionBox is ajaxSingle by default.

              • 4. Re: Cancel Button - How to bypass form validation for requir
                alan79

                ahhh.. found the root cause myself.

                The problem was the
                <a4j:support event="onselect">

                After adding the immediate attribute here as well it worked perfectly. Thank you again!

                Regards
                Alan

                • 5. Re: Cancel Button - How to bypass form validation for requir
                  alan79

                  Thank you very much ilya. I didn't see your answer when posting my solution! ;-)
                  Regards
                  Alan

                  • 6. Re: Cancel Button - How to bypass form validation for requir
                    alan79

                    immediate attribute causing me problems.

                    Usecase:
                    I have a rich:dataTable containing a list of records. When clicking a row I render the details of the selected row providing a save and a cancel button.
                    If I add the attribute required="true" to the fields I have the problem that the cancel button is validating the fields. Therefore I add the immediate="true" attribute to the button.
                    A user selects a row and starts to modify fields and clicks cancel afterwards. Now I am re-selecting the rows to discard the changes from the rich:dataTable. The backingbean is processing the reselect but the data changes are not promoted to the rich:dataTable.

                    It seems like the immediate attribute is not working properly together with
                    t:saveState
                    I use t:saveState for those fields change by AJAX functionality. Or is the reRender-function not working properly when using immediate="true" - I don't know how to figure out if the root cause is the reRender or the saveState.

                    Thank you very much for any hints!
                    btw..
                    - I don't want to use session beans
                    - I tried to use a4j:keepAlive. But this component allows me only to keepAlive complete instances of a class -> the whole backing bean. I got a lot of serializing errors e.g. for instances of HtmlDataTable

                    • 7. Re: Cancel Button - How to bypass form validation for requir
                      ilya_shaikovsky

                      you could also play with ajaxSingle/process attributes or region component instead of immediate. Then the lifecycle phases will be executed normally but the components to be processed will be linmited.

                      • 8. Re: Cancel Button - How to bypass form validation for requir
                        alan79

                        Hi Ilya

                        Thank you very much. ajaxSingle works pefectly. I didn't understand yet what this attribute is for. Now I got it! Thanks...

                        Best regards
                        Alan

                        • 9. Re: Cancel Button - How to bypass form validation for requir
                          alan79

                          No I got it finally... ajaxSingle together with the process attribute offers all oppertunities. ajaxSingle="true" is causing AJAX to process only this component. In the process attribute it is possible to define additional component-id's to be processed beside. Therefore it is possible to control AJAX perfectly. Thanks again!

                          "alan79" wrote:
                          Hi Ilya

                          Thank you very much. ajaxSingle works pefectly. I didn't understand yet what this attribute is for. Now I got it! Thanks...

                          Best regards
                          Alan