2

I have an input form on a primefaces dialog and I have server side validation that checks the input fields when the user clicks a

<p:commandButton ajax="false" action="#{bean.validate}"/>.

At the end of my validate I return null as to stay on the same page but since this is a dialog it closes. Is there any way to keep the dialog open when there are validation issues and close when there is not?

Cœur
  • 37,241
  • 25
  • 195
  • 267
c12
  • 9,557
  • 48
  • 157
  • 253

2 Answers2

2

Didn't try it myself, but you could toggle the dialog's visibility depending on the existence of validation messages:

<p:dialog visible=”#{not empty facesContext.messages}” ...>
Matt Handy
  • 29,855
  • 2
  • 89
  • 112
1

I am a Richfaces user, and their modalPanel (the equivalent of <p:dialog> offers a showWhenRendered attribute that could be helpful in your case.

Unfortunately, the <p:dialog> does not seems to have such feature. What you can do, is to play with some JavaScript functions.

First, keep a flag (a boolean property, initialized to false) in a bean that indicate if there were validation errors or not. Let's call this flag myBean.validationError.

Now, in your page, you can add a JavaScript code that will check for this property and eventually display the dialog box. Something like that (put it at the end of the page, or at least after the dialog declaration):

<script type="text/javascript">
    if (#{myBean.validationError}) {
        // showDialog();
        myDialog.show();
    }
</script>

So in your use case, the page will be redisplay, and because there was validation errors, the flag is set to true. Then, the if statement is evaluated and will display the dialog to the user.

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273