Search code examples
jsf-2

JSF Validation error not printed in open dialog


I have to validate that a field in a form is not empty.

The problem is that the form is in a JQueryUI dialog (since I don't know how to create "windows" in JSF), so when I click the commandButton the page is refreshed and the JQuery dialog is lost. After refreshing, I can see the error message was printed in the page code by looking at the source code. If I use ajax in the commandButton then the page is not refreshed but I don't get the message printed, I got it as a sort of "Javascript alert" in the browser.

How can I get the error message without refreshing the page?

<h:inputText value="#{myController.name}" id="myname" required="true" requiredMessage="Name required">
    <f:ajax />
</h:inputText>
<h:message errorClass="..." for="myname" />

Note: This looked similar to my problem but I don't think the solution is what I'm looking for.

Note: I am not using additional libraries like Primefaces, etc


Solution

  • With the hint of the comment, I was able to solve it this way:

    • In the commandButton, use ajax with both render and execute (render only doesn't work):

                          <h:commandButton id="...." action="#{myController.doSomething()}" type="submit" value="Do something">
                              <f:ajax execute="@form" render="@form" />
                          </h:commandButton>
      
    • Create a "pageUpdate()" method in myController that is called at the end of myController.doSomething(). This way, after the actions, the page is refreshed and the dialog is gone. There are of course other ways to close the dialog, but it is convenient for me to update the page when there is no error in the dialog action, so my objects in memory are updated too.

    I think JQuery dialog messes with my styles after the ajax call returns with error, so check that out if you do it.