×
☰ See All Chapters

JSF Custom Validator <f:validator> Tag

<f:validator> tag is used to register the custom validators with UI components. Follow the below steps to implement custom validator.

  1. Create a validator class by implementing javax.faces.validator.Validator interface. You should implement the validate() method. This validate method should throw javax.faces.validator.ValidatorException constructed with javax.faces.application.FacesMessage. Below is an example implementation of Validator which validates the input to be in the format MM-1234. If input does not start with “MM-” then we are throwing ValidatorException. 

public class UserIdValidator implements Validator {

 

        @Override

        public void validate(FacesContext fc, UIComponent uic, Object obj) throws ValidatorException {

                String strId = (String) obj;

                if (!strId.startsWith("MM-")) {

                        FacesMessage fm = new FacesMessage();

                        fm.setDetail("UserId should be in the format MM-1234");

                        fm.setSeverity(FacesMessage.SEVERITY_ERROR);

                        throw new ValidatorException(fm);

                }

        }

}

 

  1. Use Annotation @FacesValidator to assign a unique name to the custom validator. This unique name should be used in facelets to use this validator. 

@FacesValidator(value = "uidValidator")

public class UserIdValidator implements Validator {

 

        @Override

        public void validate(FacesContext fc, UIComponent uic, Object obj) throws ValidatorException {

                … …

        }

}

JSF Custom Validator <f:validator> Tag Example

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>JSF_CustomValidator</artifactId>

        <packaging>war</packaging>

        <version>1.0-SNAPSHOT</version>

        <dependencies>

                <dependency>

                        <groupId>com.sun.faces</groupId>

                        <artifactId>jsf-api</artifactId>

                        <version>2.2.6</version>

                </dependency>

                <dependency>

                        <groupId>com.sun.faces</groupId>

                        <artifactId>jsf-impl</artifactId>

                        <version>2.2.6</version>

                </dependency>

        </dependencies>

        <build>

                <finalName>JSF_CustomValidator</finalName>

                <plugins>

                        <plugin>

                                <groupId>org.apache.maven.plugins</groupId>

                                <artifactId>maven-compiler-plugin</artifactId>

                                <version>2.3.1</version>

                                <configuration>

                                        <source>1.9</source>

                                        <target>1.9</target>

                                </configuration>

                        </plugin>

                </plugins>

        </build>

</project>

web.xml

<!DOCTYPE web-app PUBLIC

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 "https://java.sun.com/dtd/web-app_2_3.dtd" >

 

<web-app>

        <display-name>Archetype Created Web Application</display-name>

        <servlet>

                <servlet-name>Faces Servlet</servlet-name>

                <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

                <load-on-startup>1</load-on-startup>

        </servlet>

        <servlet-mapping>

                <servlet-name>Faces Servlet</servlet-name>

                <url-pattern>*.xhtml</url-pattern>

        </servlet-mapping>

</web-app>

HelloBean.java

package com.java4coding;

 

import javax.faces.bean.ManagedBean;

 

@ManagedBean

public class HelloBean {

        private String userId;

        private String name;

 

        public String getUserId() {

                return userId;

        }

 

        public void setUserId(String userId) {

                this.userId = userId;

        }

 

        public String getName() {

                return name;

        }

 

        public void setName(String name) {

                this.name = name;

        }

 

        public String display() {

                return "second";

        }

}

UserIdValidator.java

package com.java4coding;

 

import javax.faces.application.FacesMessage;

import javax.faces.component.UIComponent;

import javax.faces.context.FacesContext;

import javax.faces.validator.FacesValidator;

import javax.faces.validator.Validator;

import javax.faces.validator.ValidatorException;

 

@FacesValidator(value = "uidValidator")

public class UserIdValidator implements Validator {

 

        @Override

        public void validate(FacesContext fc, UIComponent uic, Object obj) throws ValidatorException {

                String strId = (String) obj;

                if (!strId.startsWith("MM-")) {

                        FacesMessage fm = new FacesMessage();

                        fm.setDetail("UserId should be in the format MM-1234");

                        fm.setSeverity(FacesMessage.SEVERITY_ERROR);

                        throw new ValidatorException(fm);

                }

        }

}

first.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="https://www.w3.org/1999/xhtml"

        xmlns:h="https://xmlns.jcp.org/jsf/html"

        xmlns:f="https://xmlns.jcp.org/jsf/core">

 

<h:head></h:head>

<h:body>

<h:form>

        <h:panelGrid columns="3">

                <h:outputText value="Enter your ID" />

                <h:inputText id="uid" value="#{helloBean.userId}" required="true"

                        requiredMessage="Enter UserID">

                        <f:validator validatorId="uidValidator" />

                </h:inputText>

                <h:message for="uid" style="color:red" />

 

                <h:outputText value="Enter your name" />

                <h:inputText id="nme" value="#{helloBean.name}" required="true"

                        requiredMessage="Enter Name" style="height : 18px; width : 147px;"/>

                <h:message for="nme" style="color:red" />

 

                <h:commandButton value="Login" action="#{helloBean.display}" />

                </h:panelGrid>

        </h:form>

</h:body>

</html>

second.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="https://www.w3.org/1999/xhtml"

        xmlns:h="https://xmlns.jcp.org/jsf/html"

        xmlns:f="https://xmlns.jcp.org/jsf/core">

 

<h:head></h:head>

<h:body>

        <h1>

                <h:panelGrid columns="2">

                        User ID :

                        <h:outputText value="#{helloBean.userId}" />

               

                        Name :

                        <h:outputText value="#{helloBean.name}" />

                </h:panelGrid>

        </h1>

</h:body>

</html>

Eclipse project directory structure

jsf-custom-validator-0
 

Output

jsf-custom-validator-1
 
jsf-custom-validator-2
 
jsf-custom-validator-3
 

All Chapters
Author