casolorz
casolorz

Reputation: 9544

Keep p:dialog open when a validation error occurs after non-ajax submit

I have a PrimeFaces <p:dialog>. The dialog has an input field and a command button. The command button posts the form without ajax (e.g. to upload a file via simple mode). If there is a validation error on the input field then the <p:message> in the dialog shows the error correctly, however the dialog closes because of the non-ajax postback. Because I am not using ajax, I can't really use the oncomplete trick to keep the dialog open as answered in Keep p:dialog open when a validation error occurs after submit.

What are my options to keep the dialog open (or reopen) after non-ajax submit if there was a validation error?

Upvotes: 2

Views: 2048

Answers (1)

BalusC
BalusC

Reputation: 1108567

Make use of the dialog component's visible attribute. If this is set to true during render, then the page will render with the dialog opened.

E.g. if there's a postback and validation has failed:

<p:dialog ... visible="#{facesContext.postback and facesContext.validationFailed}">

Or if there are more forms in the same page and you'd like to check only if dialog's own form is been submitted:

<p:dialog ... visible="#{dialogForm.submitted and facesContext.validationFailed}">
    <h:form binding="#{dialogForm}">

Note: the #{facesContext.validationFailed} works by default of course only if you make use of JSF builtin validation facilities (required="true", <f:validateXxx>, etc) and/or use fullworthy Validator implementations. It you're for example manually validating in action method and manually adding faces messages, then this will not work, unless you implicitly call FacesContext#validationFailed() yourself.

See also:

Upvotes: 4

Related Questions