Updated February 4, 2023
Introduction to JSF Validation
JSF validation is the third phase in the JSF life cycle which governs everything in JSF. That is, JavaServer Faces offers a standard class set and related tags which helps in validating the data elements. In order to validate the data, there are different approaches.
- JavaServer Faces validation using backing beans.
- JavaServer Faces validation using the interface Validator.
- JavaServer Faces validation using standard validators.
Let us see how JSF validation works and what are the different validators in the below sections.
Why Do We Use JSF Validation?
Normally, if a form data is submitted to the server in web applications based on JSF, the data request parameters will be taken by the container and stores in the request object. From this request object, context will be taken and get stored in the component tree. These things are happening in the phase “Apply request values” in the lifecycle. After this, in the “update model” phase, JSF stores the data in the model object taken from the component tree object. Once all these are completed, data gets used in the application as part of the phase “Invoke application”. Exceptions won’t occur if the data offered by the user is proper while the application is executed. However, if the data received is improper, a lot of exceptions can occur. In order to overcome this issue, data has to be checked whether it is valid or not in the application logic. For that JSF validation is done. In web applications, these validations can be done on the client-side or server-side.
As already discussed, there are several approaches to validate JSF. Let us see each of them in detail.
JavaServer Faces Validation using Standard Validators:
For validation logic, code need not be written in Java Server Faces, if supplied standard validators are used. In addition to that, the standard validator tag has to be used inside a tag. That tag can be of your own choice and it denotes a User Input Interface component (or a subclass of User Input Interface component) and offers the necessary constraints.
Following are the JSF supplied standard validators.
1. LengthValidator
Tag: f:validateLength
Function: Checks whether the value length is between a range mentioned. Moreover, the value has to be java.lang.String type.
Attributes and Description:
- Minimum: The value with the minimum number of characters.
- Maximum: The value with the maximum number of characters.
These values depend on the user’s requirements.
Syntax:
<f:validatelength minimum="3" maximum="13"/>
2. LongRangeValidator
Tag: f: validateLongRange
Function: Checks whether the component value length is between a range mentioned. Moreover, the value has to be a string or numeric that is possible to convert to long.
Attributes and Description:
- Minimum: The minimum value with a particular range.
- Maximum: The maximum value with a particular range.
These values depend on the user’s requirements.
Syntax:
<f:validateLongRange minimum = "3" maximum = "100" />
3. DoubleRangeValidator
Tag: f: validateDoubleRange
Function: Checks whether the component value length is between a range mentioned. The value has to be numeric or anything that is possible to convert to numeric. Moreover, the value has to be float or that has to be possible to convert to float.
Attributes and Description:
- Minimum: The minimum value with a particular range.
- Maximum: The maximum value with a particular range.
These values depend on the user’s requirements.
Syntax:
<f:validateDoubleRange minimum = "1003.50" maximum = "20000.50" />
4. RegexValidator
Tag: f: validateRegex
Function: Checks whether the component local value is matched with a particular regular expression. The regular expression is inherited from the package java.util.regex.
Attributes and Description:
Pattern: Helps in formatting the pattern.
Syntax:
<f:validateRegex pattern = "((?=.*[a-z]).{4,})" />
5. RequiredValidator
Tag: f: validateRequired
Function: Checks whether the component local value is not empty on the component EditableValueHolder.
Attributes and Description:
- Binding: A value expression will be bind that helps in evaluating the RequiredValidator instance.
- For: Helps in referring to the exposed object value that is present in the component where this tag nests.
- Id: A component identifier which is unique.
- class: Cascade Style Sheet class will be represented.
Since this validator checks whether the field is empty, the syntax will be as shown below.
Syntax:
<h:inputText id="password" value="#{u.password}"
<f:validateRequired/>
JavaServer Faces Validation Using Backing Bean Methods:
JSF validation using the backing bean method helps in the validation of input fields of any type. The input component of Java Server faces comes with an approach where the user can bind a backing bean method to undergo pure validation. Moreover, there will be a structure that helps in grouping the validation logic even though it cannot be used for other applications.
Syntax:
<h:inputText value="#{bean.uname}" validator="#{bean.validateuname}"/>
Inside the backing bean, a method has to be declared as below.
public void validateAge(FacesContext c, UIComponent comp, Object val) {
// business validation
. . }
JavaServer Faces Custom validation components using Validator Interface:
In certain situations, a validator has to be created by the user as JSF built in validation is not enough for component value. For example, in the case where an email text is valid, an email validator has to be created. Similarly, it can be applied in other cases such as username, password, etc. The main advantage of a custom validator is that error message can be displayed.
In order to implement the JSF custom validator, following four steps can be used.
- Creation of validator class that implements the interface validator javax.faces.validator.Validator.
- Implementation of the method validate( ).
- Assignment of specific ID using @FacesValidator annotation.
- Reference the created validator class to the JavaServer Faces component through f: validator tag.
Syntax:
@FacesValidator("com.itwik.testsample")
Public class sample implements validator
{ @Override
Public void validate(FacesContext c, UIComponent cmp. String val) throws ValidatorException
{ . . . }
. . }
Conclusion
In this article, JavaServer Faces validation using several approaches such as backing beans, interface validator and standard validators are discussed in detail. As already mentioned, JavaServer Faces validation is the third phase in the lifecycle of JSF that manages each and every detail. Moreover, the purpose of validation of JSF components are also explained in this document.
Recommended Articles
This is a guide to JSF Validation. Here we discuss a brief overview of JavaServer Faces validation and its different types along with its Examples. You can also go through our other suggested articles to learn more –