3 Replies Latest reply on Sep 23, 2008 3:57 PM by newlukai

    Ignore form field validation

    newbie000

      I have a scenario where a user can click a New button, and a panel (initially not rendered) is marked to be rendered.


      This rendered panel has a bunch of fields (marked as required fields), and a New and Cancel button. The cancel method basically calls a backing bean method to set a showNewPanel field to false, in turn hiding the panel. However, when I try clicking Cancel with some of these fields remaining empty, form validation kicks in and wont hide the panel. If all the fields are populated, then it works a treat.


      My question, is there a way to overcome this, like temporarily ignoring form validation?


      Cheers.

        • 1. Re: Ignore form field validation
          jazir1979

          Yes, you can use the immediate=true attribute on your button.  I'd recommend reading up on the JSF lifecycle so you understand this, but it basically means the invoke action phase is processed immediately, and the process validations and update model phases are skipped.

          • 2. Re: Ignore form field validation
            newbie000

            Perfect, thanks.

            • 3. Re: Ignore form field validation

              It seems that I'm the only one having trouble implementing this with RichFaces.


              I've a form like this:


              <h:form id="browser">
                  <h:panelGrid cellpadding="5" cellspacing="0" columns="2">
                      <h:outputText value="#{msgs.name_lbl}:" />
                      <h:inputText value="#{browser.name}" />
              
                      <h:outputText value="#{msgs.version_lbl}:" />
                      <h:inputText value="#{browser.version}" />
              
                      <h:outputText value="#{msgs.description_lbl}:" />
                      <h:inputTextarea value="#{browser.descr}" rows="5" cols="50" />
              
                      <a4j:commandButton value="Save" action="#{adminBrowser.save}" reRender="browser, browsers" />
                      <h:commandButton value="Cancel" action="#{adminBrowser.cancel}" immediate="true" type="reset" />
                  </h:panelGrid>
                      </h:form>
              
                      <h:form id="browsers">
                  <rich:dataTable
                      value="#{browsers}"
                      var="iterBrowser"
                      id="table_browsers">
                      
                      <rich:column>    
                  <f:facet name="header">
                      <h:outputText value="Name"/>
                  </f:facet>
                  
                  <h:outputText value="#{iterBrowser.name}" />
                      </rich:column>
                       
                      <rich:column>    
                  <f:facet name="header">
                      <h:outputText value="Version"/>
                  </f:facet>
                  
                  <h:outputText value="#{iterBrowser.version}" />
                      </rich:column>
                       
                      <rich:column>    
                  <f:facet name="header">
                      <h:outputText value="Edit"/>
                  </f:facet>
                  
                  <a4j:commandButton value="Edit" action="#{adminBrowser.edit}" reRender="browser, browsers">
                      <a4j:actionparam name="selectedBrowser" value="#{iterBrowser}" assignTo="#{adminBrowser.selectedBrowser}" />
                  </a4j:commandButton>
                      </rich:column>
                       
                      <rich:column>    
                  <f:facet name="header">
                      <h:outputText value="Delete"/>
                  </f:facet>
                  
                  <a4j:commandButton value="Delete" action="#{adminBrowser.delete}" reRender="browser, browsers" />
                      </rich:column> 
                  </rich:dataTable>
                  
                  <rich:datascroller for="table_browsers" />
              </h:form>



              And what I want to do is quite common: show a list of entities, let the user add a new entity, let the user edit an entity, let the user delete an entity. It works fine. The only thing that doesn't work as exprected is the cancel button.
              I don't know how I could implement this simple usecase. The user clicks on Edit to be able to edit the selected item in the form above. Now he deletes all the content of a field and this field is bound to a bean property that is marked @NotNull and @Length(min=1). As soon as he clicks the Save button validation fails. If he clicks on Cancel nothing happens.


              Could someone help me with this issue, please?


              Thanks in advance
              Jens