Sunday, May 18, 2014

How to keep dialog open when exception and validation failed

Assuming I have the following <p:dialog />

with the following codes
<p:dialog header="Email Check" widgetVar="dgt" id="dgt">            
    <table>
        <tr>
            <td>email</td>
            <td><p:inputText value="#{testBean.email1}"></p:inputText></td>
        </tr>
        <tr>
            <td>confirm email</td>
            <td><p:inputText value="#{testBean.email2}"></p:inputText></td>
        </tr>
        <tr>
            <td> </td>
            <td>
                <p:commandButton value="submit" oncomplete="dgt.hide()"
                    action="#{testBean.checkEmail}" />
            </td>
        </tr>
    </table>        
</p:dialog>

User enters both emails, and click Submit, and dialog close.
The checkEmail action will check the validity for both of the emails entered by user when user clicked Submit.

But the problem is, when there is any exception or validation failed, the dialog still close.

To resolve this problem, the solution is to remove the oncomplete and let the action to close the dialog when exception passed.
<p:commandButton value="submit" oncomplete="dgt.hide()"
               action="#{testBean.checkEmail}" />

move the dgt.hide() into action, and let the action to close the dialog when validation passed.
public void checkEmail() {
    ......
    ......
    ......
    if(validation passed) {
        RequestContext.getCurrentInstance().execute("dgt.hide()");
    }
}


Done!!

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...