1) Introduction
This article explains about the Validator tags in JSF. In JSF, Data from the Clients, usually Browser, has to be validated before being processed by the Server Web Application. JSF has a number of Built-in Validations and also it comes with a Powerful Validation Framework for plugging-in Custom Validators. Bindings between JSF UI Components and Validation Logic can be done with ease with the help of JSF Core Tags. Four JSF Core Tags are available for doing Validation on the JSF UI Components. They are,
- Validate Length Tag
- Validate Long Range Tag
- Validate Double Range Tag
- Validator Tag
Let us see the various Validation Tags in greater detail.
2) Validate Length Tag
Validation pertaining to Data between Boundaries is a normal requirement for almost any kind of Web Application. For example, a common case includes “Password must be of [8-16] characters length”. Validation Length Tag as identified by <f:validateLength> is used to specify the maximum and the minimum characters, a JSF UI Component can accept.
Following is the syntax definition of the Validate Length Tag,
<f:validateLength minimum = "minRange" maximum = "maxRange"> < /f:validateLength>
In the above syntax, the value for the minimum and the maximum attributes must be numbers, which tells the Boundary Range allowed for a particular UI Component. For example, if a number of characters allowed for a username Text Component must be greater than 10 and less than 15, then we can have the following code which will achieves this,
<h:inputText value = "#{UserBean.userName}" id = "userNameTextField" required="true"> <f:validateLength minimum = "10" maximum = "15"/> </h:inputText>
In the above code, UserBean refers to a Managed Bean being defined and declared in the web.xml file and userName is the property of the UserBean class. The attribute ‘id’ of the <h:inputText> specifies the identifier of the Text Field UI Component and it must be unique among the other Components in the Form.
3) Validate Long Range Tag
This Tag is used to do Validations on JSF UI Components whose value is expected to fall between certain integer (long) values. Common cases will be restricting the age of a user between 1 and 40. The Validate Long Range Tag is identified by <f:validateLongRange> tag.
Following is the syntax definition of the Validate Long Range Tag,
<f:validateLongRange minimum = "minLongValue" maximum = "maxLongValue"> </f:validateLongRange>
In the above syntax, the ‘minimum’ and the ‘maximum’ attributes of Validate Long Range specifies the minimum and the maximum of the long value. Assuming that the birth-date of an Year Text-Field UI Component must fall between 1960 and 1985, then we can have the following code to achieve this goal,
<h:inputText value = "#{UserBean.birthYear}" id = "birthYearTF" required = "true"> <f:validateLongRange minimum = "10" maximum = "15"/> </h:inputText>
4) Validate Double Range Tag
This is the same the Validate Long Range Tag except that it operates on floating Data rather than Integer Data. UI Components that accepts floating values can use this Tag to perform Range Validations. The syntax for the following Tag is given by,
<f:validateDoubleRange minimum = "minDoubleValue" maximum = "maxDoubleValue"> </f:validateDoubleRange>
In the above syntax declaration, the ‘minimum’ and the ‘maximum’ attributes refer to the lower and the upper range of double value, a JSF UI Component can accept. For example, the following example restrict the Text UI Component to have values between 1000.00 and 100000.00.
<h:inputText value = "#{UserBean.amount}" id = "amountTextField" required = "true"> <f:validateDoubleRange minimum = "1000.00" maximum = "100000.00"/> </h:inputText>
5) Validator Tag
For performing Customized Validations on UI Components, one can prefer using this Validator Tag. Examples may include validating whether the entered email-id is in the appropriate format ([email protected]) or whether the stock symbol is available in the database. The following is the syntax for the Validator Tag.
<f:validator validatorId = "IdForValidator"> </f:validator>
In the above syntax, the value for the ‘validatorId’ attribute refers to a name which must be previously defined in the web.xml file. For example, if we want to do a Phone-Number Validation for a Text Component, then we would have written a class called PhoneNumberValidator which implements the javax.faces.validator.Validator interface and then overriding the Validator.validate() method. Following is the how the Class may looks like,
PhoneNumberValidator.java
package myvalidators; import javax.faces.validator.*; public class PhoneNumberValidator implements Validator{ public PhoneNumberValidator() { } public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { String strValue = (String)value; if (strValue.length() != 10){ throwException("Number of phone digits must be 10"); } } }
Then, this Customized Validator Component must be registered to the Web Application by making the following entry in the faces-config.xml file.
faces-config.xml
<webapp> <validator> <validator-id>PhoneNumberValidator</validator-id> <validator-class>myvalidators.PhoneNumberValidator</validator-class> </validator> </web-app>
Then the defined Validator Id in the faces-config.xml file, in the form of <validator-id> has to be referenced in UI Component like this,
<h:inputText value = "#{UserBean.phoneNumber}" id = "phoneNumberTF" required="true"> <f:validator validatorId = "PhoneNumberValidator"/> </h:inputText>
Javabeat has published many of the good articles related to Java Server Faces (JSF). This list will help you to learn the basic concepts on JSF and how to start using in your projects, please have a look on these articles when time permits. Don’t forget to leave your feedback on the comments section. This helps us to provide better content for you.
- JSF Interview Questions
- Request Processing Lifecycle phases in JSF
- Accessing Web Services from JSF applications
- Navigation model in JSF
The above article would provide the higher level of understanding on each concept. If you would like to know all the features in detail, please buy any of the JSF books listed in the book recommendations for Java Server Faces (JSF). Hope this helps you!!
If you would like to receive future mails on Java articles, please subscribe here.