I was trying out a simple JSF application, in which I need to check if the “name” field is blank, then display an error message.
The code which takes the field’s value is:
<h:outputLabel value="Name"/>
<h:inputText value="#{greeting.name}" required="true">
<f:validator validatorId="NumValidator"/>
</h:inputText>
The control of the program does not go into the validator class, if the field is submitted without entering anything, and it displays the default error message:
j_id_jsp_869892673_1:j_id_jsp_869892673_4: Validation Error: Value is required.
How do i display a custom message for this ?
The
Message.propertiesfile stored the default validation messages. This file is contained in the JAR of the JSF implementation library.If you look at the content of this file, regarding the required validation, you will see that:
To define your own error messages, create a new properties file, add the adequate error message, for example:
(note that
{0}will be replaced by the ID of the field)then, in your
faces-config.xml, define a newmessage-bundle:So for example, if your
my-messages.propertiesis stored in the foo/bar package, you will have to write:(note that you will not have to specify the
.propertiesextension)