title: How to validate and if successful open a new tab using JSF 2.0 with Primefaces 3.5?
body: <p>So I've read several solutions, some of which have gotten me close but not close enough to the behavior I need to implement.</p>
  
  <p>It feels like it should be simple and maybe it is but I am pretty new to JSF and Primefaces so it is not so easy for me.</p>
  
  <p>DESIRED BEHAVIOUR: Upon a click on the 'Run Report' button, validate first if successful execute a method that renders a report in a new tab otherwise stay in the same view and display some error messages.
  See below my code. Feel free to make suggestions point out mistakes, bad practices. The more feedback the better. I would appreciate actual answers rather than links though. I've been through a lot of questions on SO and could not find an answer that would solve my problem.</p>
  
  <p>The issues I'm having are:</p>
  
  <p>1- I cannot override the target attribute of the form so it opens in a new tab.</p>
  
  <p>2- I cannot call Bean method <code>runReport</code> from within the javascript function <code>handleComplete</code>. I get a 'click' property is not defined ERROR.</p>
  
  <p>Thanks talented programmers of Stack Overflow!!!!!</p>
  
  <p>FILE: report_input.xhmtl</p>
  
  <pre><code>      &lt;h:form id="pieForm" target="_self"&gt;
          &lt;p:growl  widgetVar="growlMsg" id="msgs" showDetail="true" sticky="true" /&gt;
          &lt;p:panel header="Report Criteria"&gt;
          &lt;h:panelGrid columns="5" columnClasses="col-top-style"&gt;
            &lt;h:outputLabel value="Date Range:" /&gt;
            &lt;h:outputLabel value="From:" /&gt;
            &lt;h:outputLabel value="To:" /&gt;
            &lt;p:calendar id="startDate" value="#{deficiencyBean.startDate}" mindate=""
         maxdate="#{deficiencyBean.todayDate}" readOnlyInputText="true" size="18" showButtonPanel="true"  popupIconOnly="true" mode="popup" showOn="button"  pattern="MM/dd/yyyy" /&gt;
            &lt;p:calendar id="endDate" value="#{deficiencyBean.endDate}" mindate="" 
         maxdate="#{deficiencyBean.todayDate}" readOnlyInputText="true" size="18" showButtonPanel="true"  popupIconOnly="true" mode="popup" showOn="button" pattern="MM/dd/yyyy" /&gt;
          &lt;/h:panelGrid&gt;
          &lt;h:panelGrid id="submitButton" columns="3" columnClasses="col-top-style"&gt;
          &lt;p:commandButton icon="ui-icon-check" update="msgs" action="#{deficiencyBean.runReport}" oncomplete="handleComplete(xhr, status, args) return false;" value="Run Report" /&gt;
          &lt;h:commandButton id="realSubmit" actionListener="#{deficiencyBean.runReport}" style="display: none;"/&gt;
          &lt;p:commandButton id="validateFunction" actionListener="#{deficiencyBean.validate}" rendered="false" update="msgs"/&gt; 
          &lt;p:commandButton id="hiddenValidVar" value="#{deficiencyBean.validInput}"  oncomplete="handleComplete(xhr, status, args)"/&gt;
         &lt;/h:panelGrid&gt;
        &lt;/p:panel&gt;
       &lt;/h:form&gt;
      &lt;script&gt;
      function handleComplete(xhr,status,args){
          var valid = args.isValid;
          alert(valid);
          if(valid === 'true'){
          document.getElementById('pieForm').setAttribute("target", "_blank"); // NOT WORKING
          document.getElementById('pieForm').target="_blank";//NOT WORKING
          document.getElementById('pieForm:realSubmit').click();//NOT WORKING
          }
      }
       &lt;/script&gt;
       &lt;/ui:composition&gt;
  
      &lt;/html&gt;
  </code></pre>
  
  <p>FILE: ReportBean.java</p>
  
  <pre><code>@ManagedBean(name = "deficiencyBean")
  @SessionScoped
  @SuppressWarnings("serial")
  public class deficiencyBean implements Serializable{
  private Date startDate; //public getters n setters
  private Date endDate;   //public getters n setters 
  private Boolean validInput; //public getters n setters 
  public Boolean validate(){
  //SOME LOGIC
     RequestContext.getCurrentInstance().execute("document.getElementById('pieForm').target='_blank'");//NOT WORKING
      if(validInput){
          RequestContext.getCurrentInstance().addCallbackParam("isValid", validInput);
  
      }
  }
  public void runReport(){
     //JASPER REPORTS COMPILE, FILL REPORT and DISPLAY
  }
  
  }//END OF CLASS
  </code></pre>