<h:message />
<h:messages />
Code language: HTML, XML (xml)
h:messages is used to display all messages at once. You can place h:messages tag in start of your form. You may need to display only global messages using h:messages tag. For displaying only global messages set globleOnly attribute of h:messages to true. <h:messages globalOnly="true" />
Code language: HTML, XML (xml)
Use h:message to display message attached to one component. An attibute for=”” can be used to specify the id of a component whose error messages we need to display. h:message is used to display error message next to the component that generated the error. If more then one message is attached to that component, h:message will display the last message. ...
<h:inputText id="userName" value="#{userBean.userName}" />
<h:message for="userName" />
...
Code language: HTML, XML (xml)
Each message can have a summary description and a detailed description. When using the h:message tag, the default is to show the detail message. When using the h:messages tag, the default is to display the summary descriptions. To change the defaults, modify the boolean showSummary and showDetail attributes. <h:message for="userName" showSummary="true"/>
<h:messages showDetail="true"/>
Code language: HTML, XML (xml)
You can also enable component’s detail message to appear as a tooltip. To do so, set tooltip attribute of message tag to true. Note that for enabling this option, showDetail and showSummary must be set to true. There are four forms of JSF validation: 1. Built-in validation components 2. Application level validations 3. Custom validation components using Validator interface 4. Validation methods in backing beans <h:inputText id="Username" value="#{UserBean.userName}">
<f:validateLength minimum="6" maximum="15"/>
</h:inputText>
....
<h:inputText id="Age" value="#{UserBean.age}">
<f:validateLongRange minimum="1" maximum="120"/>
</h:inputText>
Code language: HTML, XML (xml)
public void <METHOD_NAME> (FacesContext context, UIComponent component, Object value) { .. }
Code language: Java (java)
Once a backing bean method is ready we can bind it with a component using f:validator tag. <h:inputText value="#{userBean.name}" validator="#{userBean.checkUsername}">
</h:inputText>
Code language: HTML, XML (xml)
In above snippet, we have bind checkUsername() method of userBean to component inputText. It is possible to bind more than one validators to one component. Backing bean method of validation is easy to implement, but this method is specific for one application and may not be reused for different application. To create generic validators which can be used in different application, Validator interface can be used. import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
...
...
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
}
Code language: Java (java)
Once the Validator is implemented, we need to register this validator in faces-config.xml file. To do so copy following code in faces-config.xml assuming that our validator class name is net.viralpatel.jsf.helloworld.EmailValidator. <validator>
<validator-id>emailValidator</validator-id>
<validator-class>net.viralpatel.jsf.helloworld.EmailValidator</validator-class>
</validator>
Code language: HTML, XML (xml)
We can bind this validator with any component using f:validator tag. <h:inputText id="Email" value="#{userBean.email}" required="true">
<f:validator validatorId="emailValidator" />
</h:inputText>
Code language: HTML, XML (xml)
Note that in above code snippet, validatorId attribute of f:validator tag points to the validator’s ID that is registered in faces-config.xml file. For validating email address we can create a Validator class as: package net.viralpatel.jsf.helloworld;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
public class EmailValidator implements Validator{
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
String email = (String) value;
if(!email.contains("@")) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("Email is not valid.");
message.setDetail("Email is not valid.");
context.addMessage("userForm:Email", message);
throw new ValidatorException(message);
}
}
}
Code language: Java (java)
Thus by using JSF Validation framework, it is possible to validate user input easily. We saw different ways of validation in JSF: Default validators, backing bean methods and validation through validator interface. Let me know your comments about this tutorial. << PREVIOUS: Tutorial: Creating JavaServer Faces JSF application in Eclipse. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Hi,
Useful article buddy. It would really help if you can put up a post on integrating Apache Shale with JSF. Thanks. Keep up the good work!
Cheers,
John
What's up? Been reading for a while and just decided to write!
Short but very well done tutorial. Thanxs
THANKS. Very good
Very useful and to the point hints in this tutorial. Thanks for this share.
Thanks for the article. Real good one.
Nice work , keep it up ..
thanks buddy.
Hi how to generate popup while displaying validation error. for example for inputtext required=true if i skip and press submit button i want to display h:message in popup is that possible in jsf2.0 . could u repsond this .
Thanks inadvance.
Thanks for the tutorial. Very helpful for the beginners :)
thx 4 the tutoial :))
Thanks , very good for the beginner :D