5

I'm using <p:dialog>. After submitting the form therein, I use dialog.hide() and it fires the ajax close event listener method which will update a List<E>. It was working properly, but when I put some required input components and bring the <p:dialog> again if there is some validation error, it doens't fire the method anymore.

The dialog:

<p:outputPanel autoUpdate="true">
    <p:dialog id="dialogComentario" header="Deixe sua avaliação" widgetVar="confirmation" 
        showEffect="fade" hideEffect="fade" height="340" width="500" modal="true" 
        visible="#{not empty facesContext.maximumSeverity}" 
        resizable="false" closable="true" draggable="false">
        <h:form prependId="false">
            ...
            <p:commandLink styleClass="btn btn-primary btenviacoment" 
                oncomplete="if (!args.validationFailed) confirmation.hide();" 
                actionListener="#{comentario.actEnviarComentario}" global="false">
                <i class=" icon-play-circle icon-white"></i>
                <h:outputText value=" Enviar Comentário" />
                <f:param name="codigoplu" value="#{produto.produto.codigoplu}" />
            </p:commandLink>
            ...
            <p:commandLink styleClass="btn" onclick="confirmation.hide();" 
                global="false" immediate="true">
                <h:outputText value=" Cancelar" />
                <i class="icon-off"></i>
            </p:commandLink>
            ...
        </h:form>
        <p:ajax event="close" update=":avaliacoesClientes, :dialogComment" 
            listener="#{produto.atualizarComentarios}" global="false" />
    </p:dialog>
</p:outputPanel>

The action listener method:

public void actEnviarComentario(ActionEvent event) {
    String codigoplu = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("codigoplu");
    PegarDadosCliente();

    try {
        DateFormat f = new SimpleDateFormat("dd/MM/yyyy");
        java.util.Date utildata = new java.util.Date();
        utildata = (java.util.Date) f.parse(String.valueOf(data.getValue()));
        java.sql.Date datasql = new java.sql.Date(utildata.getTime());

        Comentario comentario = new Comentario(Integer.parseInt(usuario.getId()), Integer.parseInt(codigoplu), titulo.getValue().toString(), mensagem.getValue().toString(), datasql, Integer.parseInt(rating.getValue().toString()), new java.sql.Date(new Date().getTime()));
        listavelComentarios.inserirComentario(comentario);

        RequestContext.getCurrentInstance().execute("confirmation.hide();");
    } catch (NamingException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

I tried to close the dialog with RequestContext as shown in action method, but that won't fire the ajax close event either.

Here's the ajax close event listener method:

public void atualizarComentarios(CloseEvent event) {

    try {
        comentarios = comentario.listarComentarios(codigoplu);

        if (comentarios.size() > 0) {
            msgAvaliacao = "Avaliação do produto.";
            int votos = 0;

            for (Comentario comentario : comentarios) {
                votos += comentario.getAvaliacao();
            }

            rating = votos / comentarios.size();
        }
    } catch (NamingException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Victor Bello
  • 493
  • 1
  • 8
  • 23
  • Can you post `p:dialog` tag? – partlov Jan 16 '13 at 13:58
  • sorry, i missed. Updated. – Victor Bello Jan 16 '13 at 14:02
  • Is it necessary to do it in *Ajax* way? I make this question because I see you're complicating your code in some way. I think it's easier just to use `h:commandLink` with `action` methods and `ajax="false"`. That will make a submit, you can refresh what you want there and once back the dialog will not be displayed. – Aritz Jan 16 '13 at 15:05

1 Answers1

3

This problem is not related to validation. Remove all those input components and press the command button/link and you'll see that it's still not fired.

This problem is caused by the unnecessary combination of <p:outputPanel autoUpdate="true"> and visible="#{not empty facesContext.maximumSeverity}". The output panel keeps auto-updating the dialog which apparently forces you to set the visible attribute like that. The dialog is auto-updated right before the oncomplete event is fired. If the dialog visible property is false, then the dialog is already hidden (invisible) before the oncomplete is fired.

So, just get rid of the <p:outputPanel> and the visible attribute. Your oncomplete already does the right job.


Unrelated to the concrete problem, the RequestContext line is unnecessary. You're already hiding it in oncomplete which is perfectly fine. See also Keep p:dialog open when a validation error occurs after submit.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for you answer, @BalusC. But when I use the other button "Cancel" which just use 'onclick="confirmation.hide();" the close event is called. – Victor Bello Jan 16 '13 at 15:50
  • You're right. It's quite possible that this is just a bug in PrimeFaces it doesn't invoke the close event when it's currently handling a postback. I edited the answer accordingly. You might want to report it to the PF guys. – BalusC Jan 16 '13 at 15:57
  • I made another test @Balusc, and changed the "cancel" button to "oncomplete" too, and it works (still calling close event)! Also, I use to have this method called (close event), but I had problems to keep up when a validation error occurs... After I made – Victor Bello Jan 16 '13 at 16:14
  • I now see it. The `` + `visible` is causing this. As I already mentioned in my initial answer, that whole thing is completely unnecessary. But now it turns out that it actually caused the whole problem. So just removing them should fix your problem. – BalusC Jan 16 '13 at 16:20
  • Yes, I was doing the changes that you said, and everything works but clear the inputs components, any idea? when I try to open the the inputs components still have the values... – Victor Bello Jan 16 '13 at 16:26
  • Ajax-update the dialog's content before opening it. See also e.g. http://stackoverflow.com/questions/10614047/clear-hibernate-validator-messages/10618108#10618108 – BalusC Jan 16 '13 at 16:27