Raaz
Raaz

Reputation: 125

display a confirmation popup on button click based on condition

I have a p:commandButton on click of which I need to add a few values to a list. In my managed bean, I'm validating the value that has to be added, and if it validates to false, I have to display a confirmation popup. This is my code -

<p:commandButton id="add" value="Add" type="submit"                                      action="#{bean.doAdd}" ajax="false"
update=":List"/>

And in the bean, on click of the "Add" button,

public String doAdd() throws Exception {
        if(response != null) {
            if(keyList.contains(response))  {
                if(!responseList.contains(response)) {
                    responseList.add(response);
                } 
            } else {
               //Have to display confirmation popup.
            }
            response = "";
        }

        return response;
    }

I'm using jsf 2.0 and primefaces 3.0. Can someone please tell me how to display the popup from the bean?

Upvotes: 0

Views: 6343

Answers (2)

Daniel
Daniel

Reputation: 37061

You can use RequestContext to run js code inside your managed bean

Make sure its an ajax call - got no ajax="false"

like this

RequestContext context = RequestContext.getCurrentInstance();  
context.execute("YourDialogwidgetVar.show()");

I assume you got some dialog defined...

<p:confirmDialog id="confirmDialog" message="Hello"  
                header="Header" widgetVar="YourDialogwidgetVar">  
</p:confirmDialog> 

Upvotes: 2

ravindra
ravindra

Reputation: 186

This code may helpfull to you.

private boolean valid = true; 

public void doAdd() {
    valid = false;
}


<p:dialog id="basicDialog" header="Basic Dialog" visible="#{!testBean.valid}">
    <h:outputText value="Message!" />
</p:dialog>

<h:commandButton id="modalDialogButton" value="Modal" action="#{testBean.doAdd}"/>

Upvotes: 0

Related Questions