1

I'm trying to validate a textfield.

I have a class customer with a field name:

@Size(min = 3, message = "At least 3 characters")
private String name;

This is the index.xhtml:

<p:outputLabel for="name" value="#{msg.name}" />
<p:inputText value="#{customer.name}" id="name" />
<h:message for="name"/>
<p:commandButton id="submit-button" value="#{msg.next}" action="next.xhtml"/>   

When I enter a short string (with 2 letters), I get as a response:

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="j_id1:javax.faces.ViewState:0"><![CDATA[4215878805973597748:-123199067583012360]]></update><extension ln="primefaces" type="args">{"validationFailed":true}</extension></changes></partial-response>

So, apparently, it recognizes, that the entered name is too short, but I don't get any error message in the view, as expected here: <h:message for="name"/>.

Evgenij Reznik
  • 16,046
  • 33
  • 87
  • 157

2 Answers2

2

Consider the following:

<p:commandButton id="submit-button" value="#{msg.next}" action="next.xhtml"/>

The p:commandButton is a Primefaces library tag and is an enhanced version of the JSF's h:commandButton. The Primefaces button is by default ajax enabled.

In the example posted, when the commend button is pressed the form is processed (this is the default behavior) and the next.xhtml page is to be displayed.

The application is using bean validation to validate the input text; the p:inputText tag. When the user enters a 2 character value in the input text and presses the command button the validation is happening. The same page is shown but the expected validation message "At least 3 characters" is not rendered to the page.

One can see something like this in the server log (this is from Apache Tomcat's log files):

org.apache.myfaces.lifecycle.RenderResponseExecutor.execute There are some unhandled FacesMessages, this means not every FacesMessage had a chance to be rendered. These unhandled FacesMessages are: - Name: Minimum 2 characters are required!

This is because the request is originally to render the next.xhtml; this is the normal behavior. If one enters a valid name with 5 characters the next page will be displayed as expected. Due to the validation exception the current page is shown again but the message is not refreshed to the page. The message component needs to be refreshed explicitly.

Change this:

<h:message for="name"/>

To:

<h:message for="name" id="errmsg"/>

And, specify in the command button that this message component needs to be updated on submit.

Change this:

<p:commandButton id="submit-button" value="#{msg.next}" action="next.xhtml"/>

To:

<p:commandButton id="submit-button" value="#{msg.next}" action="next.xhtml" update="errmsg"/>


Alternate:

Another way of solving this is to simply submit a non-ajax request and then the full page will be refreshed by default - and one can see the validation message. In this case no need to have an "id" attribute for the h:message tag and the command button can be declared without the ajax behavior as:

<p:commandButton ajax="false" id="submit-button" value="#{msg.next}" action="next.xhtml"/>


NOTES: The above conclusion is based on code tried on Apache Tomcat 8 web server, Apache MyFaces 2.2, Primefaces 5.3 and Hibernate Bean Validator 5.2.

prasad_
  • 8,610
  • 1
  • 14
  • 24
  • Thanks for answering, Most is explained in https://stackoverflow.com/questions/25339056/understanding-primefaces-process-update-and-jsf-fajax-execute-render-attributes – Kukeltje Nov 02 '18 at 06:33
0

I think the p:commandButton submit is an Ajax one, thus you have to specifically define components you want to be updated after the query is completed.

Since you are using primefaces, here is a validation example from their showcase.

Mooolo
  • 378
  • 1
  • 7
  • As explained in this duplicate: https://stackoverflow.com/questions/18585042/validator-is-called-but-error-message-is-not-displayed – Kukeltje Nov 02 '18 at 07:16