primefaces tabView skip the form validation when tab change

heng heng picture heng heng · Feb 20, 2013 · Viewed 9.3k times · Source

I'm using PrimeFaces 3.3 and Mojarra 2.17.

I have a page that contains a p:tabView. The second tab contains a textbox and required="true". When I navigate from the second tab to first tab, it will validate the textbox is empty and display an error message (I have a PhaseListener class to display the error in global p:messages). How to skip this validation?

I can't found any solutions in:

and I do not want to use PrimeFaces p:wizard.

My tabView.xhtml:

<p:tabView id="tabView" dynamic="true" cache="false" activeIndex="0" >  
      <p:ajax event="tabChange" listener="#{test.onTabChange}"/> 
      <p:tab id="tab1" title="Tab1">
        <ui:include src="#{test.tab1}"></ui:include>
      </p:tab>
      <p:tab id="tab2" title="Tab2">  
        <ui:include src="#{test.tab2}"></ui:include>
      </p:tab>
</p:tabView>

In my Java class:

public void onTabChange(TabChangeEvent event) {              
     this.setTab1("tab1.xhtml");
     this.setTab2("tab2.xhtml");
 }

My tab1.xhtml:

<h:form id="form1">    
    <p:messages globalOnly="true" />
    tab1 page
    <br/>
</h:form>

My tab2.xhtml:

<h:form id="form2">
    <p:messages globalOnly="true" />
    <p>
        tab2 page
    </p>
    <p:inputText id="txt1" required="true" />
    <p:message for="txt1" display="text" />
    <p:commandButton value="Submit" update="@form" />
</h:form>

My PhaseListener class:

public class MyPhaseListener implements PhaseListener {
    @Override
    public void afterPhase(PhaseEvent e) {
        if (e.getPhaseId() == PhaseId.PROCESS_VALIDATIONS) {
            if (FacesContext.getCurrentInstance().getMessageList().size() > 0) {
                String number = String.valueOf(FacesContext.getCurrentInstance().getMessageList().size());
                String errorMsg = "there are " + number + " errors.";
                FacesContext.getCurrentInstance().addMessage(
                        null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMsg,
                                errorMsg));
            }
        }
    }
}

Answer

heng heng picture heng heng · Mar 22, 2013

I solved my problem by adding the code:
FacesContext.getCurrentInstance().renderResponse();
under TabChangeEvent method in backign bean..

eg:

public void onTabChange(TabChangeEvent event) {              
  this.setTab1("tab1.xhtml");
  this.setTab2("tab2.xhtml");
  FacesContext.getCurrentInstance().renderResponse();
}