Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

p:dialog gets closed on validation error of a submit with ajax="false", how to keep dialog open?

I have the following <p:dialog>

<p:dialog id="dlgDownload" header="#{appmsg['header.download.popup']}" widgetVar="downloadDlg" resizable="true" modal="true" closable="true" width="640" dynamic="false">
    <h:form id="frmDownload">
        <ui:include src="downloadDialog.xhtml" />
    </h:form>
</p:dialog>

The include file contains the following download button:

<p:commandButton id="btnDlgDownload" value="#{appmsg['action.download.label']}" title="#{appmsg['action.download.label']}" 
    icon="ui-icon-arrowthickstop-1-s" ajax="false"  oncomplete="if (!args.validationFailed){downloadDlg.hide();} else {downloadDlg.show();}" process="@this" update=":#{p:component('pnlDownload')}" >
    <p:fileDownload value="#{downloadController.downloadFile()}" />
</p:commandButton>

This uses <p:fileDownload> for downloading the file, this means I have to use ajax="false" for <p:fileDownload> to trigger.

But if there is a validation failure in the dialog, then I see that the dialog window gets closed. I want the error message to be shown in the dialog window and not in the main page.

How do I keep the dialog open, so that I can show the error message in the dialog window?


@Balusc Please find my attempt on SSCCE Basically there is a parent.xhtml where the download button resides and there is a downloadDialog.xhtml embedded on the p:dialog

   <p:messages id="globalMessages" globalOnly="true" showDetail="true"
        showSummary="true" closable="true" />
    <h:form = "parentForm" >
                    <p:commandButton id="btnDownload"
                        value="Download"
                        title="Download"
                 icon="ui-icon-arrowthickstop-1-s" onclick="downloadDlg.show();">
            </p:commandButton>
</h:form>
    <p:dialog id="dlgDownload" header="Download" widgetVar="downloadDlg" resizable="true" 
         modal="true" closable="true" width="640" dynamic="false" visible="#{frmDownload.submitted and facesContext.validationFailed}">
        <h:form id="frmDownload" binding="#{frmDownload}">
        <ui:include src="downloadDialog.xhtml" />
        </h:form>
    </p:dialog>

Inside downloadDialog.xhtml

    <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui"
        xmlns:pe="http://primefaces.org/ui/extensions">

        <p:outputPanel id="pnlDownload">
    <h:panelGrid id="dateDisplayGrid" columns="4" style="margin-bottom:10px" cellpadding="5" rendered="#{downloadForm.displayDates}">
    <p:calendar id="strtdt"  readonlyInput="true" size="12" value="#{downloadForm.startDate}" >
                </p:calendar>
                <h:outputText value="#{appmsg['label.to']}" />              
    <p:calendar id="enddt"  readonlyInput="true" size="12" value="#{downloadForm.endDate}"
                pattern="#{dateFormatting.shortDateFormat}" navigator="true"  >
                <f:validator validatorId="dateRangeValidator" />
                <f:attribute name="startDate" value=":#{p:component('strtdt')}" />          
                </p:calendar>
                <p:message id="dateError" for="enddt"  showDetail="true" showSummary="false"></p:message>           
            </h:panelGrid>
    <p:commandButton id="btnDlgDownload" value="Download" title="Download" 
                     icon="ui-icon-arrowthickstop-1-s" ajax="false"  oncomplete="if(!args.validationFailed)downloadDlg.hide();" >
    <p:fileDownload value="#{downloadController.downloadFile()}" />
                </p:commandButton>

                <p:button id="btnDlgCancel" value="#{webmsg['action.cancel']}" onclick="downloadDlg.hide(); return false" href="#" />
        </p:panel>
    </p:outputPanel>
</ui:composition>

When I hit the download button on the dialog window, the error gets displayed on the parent html and dialog remains closed. But when I hit the download button on parent page the dialog window reappears and contains the error message in the inside dialog window.

Thanks for any help.

like image 896
Sabarish Avatar asked Nov 13 '22 06:11

Sabarish


1 Answers

What if you delegate part of the downloadFile method to another method?

In this case, you would need another method (let's call it prepareDownloadFile) that would be called using ajax. Once this method is complete you can check for errors. In case everything goes well you can call a remoteCommand (with ajax=false) to send the file to the user.

Another idea would be to use iframes. Check out this post.

Hope that helps!

like image 89
Daniel Teleginski Camargo Avatar answered Nov 15 '22 11:11

Daniel Teleginski Camargo