Search code examples
jsfjsf-2primefacesdialog

Conditionally open global p:confirmDialog with p:confirm


I want to open a global p:confirmDialog based on a bean boolean value. I would like to have something like this:

<p:commandButton value="Save" actionListener="#{bean.save}" 
                 update="@form" oncomplete="jsfunction();">
    <p:confirm header="Confirm" message="Are you sure?" rendered="#{bean.boolean}"/>
</p:commandButton>

But rendered doesn't work there (I wish).

Also, I don't want to duplicate the p:commandButton and use its rendered attribute to achieve this.

Is there any way to get this done without changing too many things? I have to do it in a lot of buttons.


Solution

  • I know that this question is a bit old, but I was recently having the same problem with conditional/dynamic confirmations and none of the above solutions worked fine for me.

    After some tests, I built my button using the attribute disabled (introduced in PrimeFaces 6.0) as in:

    <p:confirmDialog global="true">
        <p:commandButton value="Yes" type="button" />
        <p:commandButton value="No" type="button" />
    </p:confirmDialog>
    
    <p:commandButton actionListener="#{myBean.myMethod} value="Submit">      
        <p:confirm header="Confirmation" message="Are you sure?" disabled="#{myBean.boolean}"/>
    </p:commandButton>