6

I have a report that needs to be opened in another tab, but only if the validation hasn't failed. In my case, the validation fails and the same page opens in another tab with the validations errors.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
shouts
  • 135
  • 2
  • 5

2 Answers2

5

Luiggi has answered the POST matter. If the report is however available by a GET request, then there are other ways.

  1. Use window.open() in oncomplete.

     <h:form>
         ...
         <p:commandButton action="#{bean.submit}" update="@form" 
             oncomplete="if (args &amp;&amp; !args.validationFailed) window.open('reportURL')" />
     </h:form>
    
  2. Or conditionally render a <h:outputScript> with window.open().

     <h:form>
         ...
         <p:commandButton action="#{bean.submit}" update="@form" />
         <h:outputScript rendered="#{facesContext.postback and not facesContext.validationFailed}">
             window.open('reportURL');
         </h:outputScript>
     </h:form>
    
  3. Or use PrimeFaces Primefaces#execute() with window.open().

     public void submit() { 
         // ...
         PrimeFaces.current().executeScript("window.open('reportURL');");
     }
    

The first way requires the URL to be already definied before submit, the latter two ways allows you to use a dynamic URL like so window.open('#{bean.reportURL}').

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I´m starting to learn JSF and PrimeFaces and wanted to know if there is some example this code? Because I have a doubt if this example can work in my case. I have a form that user enter two dates and submit this form to show records in a datatable in other form. But needed to be validate. – Diego Macario Nov 12 '13 at 15:53
1

I don't know if this is a definitive answer, but you could have 2 for this: 1 that will trigger the server validation and other that will call the report in a new page. Here's a start code:

<h:form id="frmSomeReport">
    <!-- your fields with validations and stuff -->

    <!-- the 1st commandLink that will trigger the validations -->
    <!-- use the oncomplete JS method to trigger the 2nd link click -->
    <p:commandLink id="lnkShowReport" value="Show report"
        oncomplete="if (args &amp;&amp; !args.validationFailed) document.getElementById('frmSomeReport:lnkRealShowReport').click()" />

    <!-- the 2nd commandLink that will trigger the report in new window -->
    <!-- this commandLink is "non visible" for users -->
    <h:commandLink id="lnkRealShowReport" style="display:none"
        immediate="true" action="#{yourBean.yourAction}" target="_blank" />

</h:form>
<!-- a component to show the validation messages -->
<p:growl />

In order to check if the validation phase contained any error, check How to find indication of a Validation error (required=“true”) while doing ajax command

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332