JSF validator Tag

JSF validator Tag is used to add and register the validator to the nearest parent component.

JSF validator Tag

JSF validator Tag

        

This tag is used to add and register the validator to the nearest parent component. Typically all applications requires filling some or more information in the page. So it will be better approach to find out the invalid information value or format as soon as possible to make it user friendly. So user can find out faults and correct it. JSF provides built-in validation rules. JSF also helps to customize the validation rules to make it  according to the need. If the component has input value then the conversion of the value to the required type is needed and then specified validator type is invoked to check whether this value is of required format and type. This tag contains one attribute named "validatorID" which is required attribute. In this attribute we specify the name of backing bean class where we write functionality according to our need.For custom validation Validator interface is implemented in the class providing validation. You have to maintain the faces-config file where validator-id and validator-class is specified within the validator element. Here, one thing that we have to take care is that validator-id in both places (in faces-config file and validator tag in jsp file ) should match. 

Code Description :

<%@ page contentType="text/html" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<f:view>
<html>
<body>
<h:form id="form1">
<table>
<tr>
<td><font color="#FF0000"><h:message for="email" /></font></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>
<h:inputText id="email" required="true">
<f:validator validatorId="checkvalidemail" />
</h:inputText>
</td>
<td><h:commandButton value="Submit" /></td>
</tr>
</table>
</h:form>
</body>
</html>
</f:view>

Backing Bean (Validation.java) : The code below is for backing bean used in this example. 

package roseindia;

import javax.faces.*;
import javax.faces.validator.*;
import javax.faces.application.*;
import javax.faces.component.*;
import javax.faces.context.*;
import java.util.regex.*;
public class validation implements Validator{
public validation(){}

public void validate(FacesContext facesContext, UIComponent uIComponent, Object object) throws ValidatorException{
String enteredEmail = (String)object;
Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
Matcher m = p.matcher(enteredEmail);
boolean matchFound = m.matches();

if (!matchFound) {
FacesMessage message = new FacesMessage();
message.setSummary("Invalid Email ID.");
throw new ValidatorException(message);
}
}
}

Now we have to register this class in the faces-config file like below :

<faces-config>
..................
..................
<validator>
<validator-id>checkvalidemail</validator-id>
<validator-class>roseindia.validation</validator-class>
</validator>
.................
.................
</faces-config>

Here in this above case "checkvalidemail" specified in "validator-id" is the alias name (this will be used in jsp file to access the actual backing bean class)  of the actual class specified in "validator-class" in the faces-config file. For ex.

Rendered Output :

If we enter incorrect email address then its validation process makes it invalid and shows error message to the user, like below.

Html Source Code :

<html>
<body>
<form id="form1" method="post" action="/f-tags/pages/validator/validator.jsf" enctype="application/x-www-form-urlencoded">
<table>
<tr>
<td><font color="#FF0000"></font></td>
<td>&nbsp;</td>
</tr><tr><td>
<input id="form1:email" type="text" name="form1:email" />
</td>
<td><input type="submit" name="form1:_id1" value="Submit" /></td>
</tr>
</table>

<input type="hidden" name="form1" value="form1" /></form>
</body>
</html>

This tag contains one attribute :

validatorId : This is the required attribute and  is used to specify the ID of the validator class which we are going  to use in this validation process (in the case of custom validation). This value must match with the validator-id element specified in validator tag of faces-config file.