Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use target=“_blank” to open report in new window only if validation has not failed

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.

like image 448
shouts Avatar asked Jan 29 '13 13:01

shouts


2 Answers

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}').

like image 147
BalusC Avatar answered Sep 27 '22 23:09

BalusC


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

like image 32
Luiggi Mendoza Avatar answered Sep 27 '22 22:09

Luiggi Mendoza