Dominik Sandjaja
Dominik Sandjaja

Reputation: 6476

Show dialog in case of error

I am using JSF with PrimeFaces 3.2 on a JBoss AS 7.1.

The problem: I can't manage to hand over verification results (not the regular JSF validation!) from my backend to my frontend.

I have a form which is bound to an entity and when clicking on the save button, I want to do some verification of the entity:

<p:growl id=saveSuccessDialog" />

<p:dialog widgetVar="verificationDialog" modal="true"
    header="#{msg['dialog.title.verificationError']}" resizable="false">
    <h:form id="verificationErrorForm">
        <h:outputText value="#{verificationResult.problemDescription}" />
    </h:form>
</p:dialog>

<h:form>
...some fields...
<p:commandButton
    actionListener="#{adminBean.saveObject}"
    value="Save" icon="ui-icon-check"
    onerror="verificationDialog.show();"
    update=":verificationErrorForm :saveSuccessDialog" />
</h:form>

Now, the bean's action is like this:

public void saveObject(ActionEvent event) {
    this.verifyObject();
    if (verificationResult.isValidConfiguration()) {
        FacesContext.getCurrentInstance().addMessage("Object successfully saved", new FacesMessage("Saved")); // this is correctly shown in the growl
        objectDao.save(this.sessionBean.getSelectedObject());
    } else {
        FacesContext.getCurrentInstance().getExternalContext().setResponseStatus(500);
    }
}

And the VerificationResult:

@Named
@ConversationScoped
public class VerificationResult implements Serializable {
    private String problemDescription; // with get/set
    ... some more information about the problem here ...
}

I hope, the intention is clear: When the user clicks "save", the verification will run (that works) and if the verification fails, a dialog should pop up with additional information. If the verification succeeds, the "save successful" growl appears. That works, as well.

But if the verificationError-dialog gets displayed, the verificationResult.problemDescription is empty although it got set.

So, two questions: a) how can I solve that? b) is there a better solution to handle such requirements?

I hope, the question is understandable. Thanks in advance!

Upvotes: 2

Views: 3712

Answers (1)

Dominik Sandjaja
Dominik Sandjaja

Reputation: 6476

After a few more hours of searching I finally found a solution at the PrimeFaces forum: http://forum.primefaces.org/viewtopic.php?f=3&t=16769

The basic idea is to schedule the evaluation, whether the dialog should be shown or not, to the visible-attribute of the dialog itself and then let the dialog get updated in the update-attribute of the commandButton/Link.

Thanks to BalusC who asked that question over at the other forum :-)

Upvotes: 2

Related Questions