JSF - Using Custom Converter and Validator for the same Input component

[Updated: Sep 15, 2017, Created: Sep 13, 2017]

This example shows how to use a custom converter and a custom validator for the same input component. The converter is invoked before the validator. The value converted by the converter is passed to the validator for validation. This example will demonstrate the distinct responsibilities of the converter and the validator.


public class Employee {
  private long id;
  private String name;
  private String department;
  private String role;

Creating a Converter

This custom converter will return an employee object either for an input of employee id or for an input of employee name. The 'by' property will be the attribute of the validator tag.

public class EmployeeConverter implements Converter {
  private String by;

  public Object getAsObject(FacesContext context, UIComponent component, String value) {
      if ("name".equalsIgnoreCase(by)) {
          return EmployeeService.Instance.findByName(value);
      } else if ("id".equalsIgnoreCase(by)) {
          return EmployeeService.Instance.findById(value);
      return null;

  public String getAsString(FacesContext context, UIComponent component, Object value) {
      Employee employee = (Employee) value;
      if ("name".equals(by)) {
          return employee.getName();
      } else if ("id".equals(by)) {
          return Long.toString(employee.getId());
      return null;

  public String getBy() {
      return by;

  public void setBy(String by) { = by;
public enum EmployeeService {
  private List<Employee> list = new ArrayList<>();

      list.add(new Employee(100, "Sara", "Admin", "Manager"));
      list.add(new Employee(200, "Mike", "IT", "Developer"));

  public Employee findById(String idString) {
      try {
          long id = Long.parseLong(idString);
                     .filter(e -> e.getId() == id)
      } catch (NumberFormatException e) {
          return null;

  public Employee findByName(String name) {
                 .filter(e -> e.getName().equals(name))

The above converter returns null for invalid id or invalid name instead of throwing an exception. That will be the validator responsibility to throw ValidatorException for the invalid input; the validator will treat a null value for the employee as an invalid input.

Creating a Validator

The Employee object returned from our above validator will be passed as input value to this custom validator. Here the validation constraint is that employee object must not be null or must have the role as specified with the validator's attribute 'role' value.

public class EmployeeValidator implements Validator {
  private String role;

  public void validate(FacesContext context, UIComponent component,
                       Object value) throws ValidatorException {
      String errorMessage = null;
      if (!(value instanceof Employee)) {
          errorMessage = "No employee found.";
      } else if (!((Employee) value).getRole().equalsIgnoreCase(role)) {
          errorMessage = "Employee does not have the role: " + role;

      if (errorMessage != null) {
          FacesMessage msg =
                  new FacesMessage("Invalid Employee Input", errorMessage);
          throw new ValidatorException(msg);

  public String getRole() {
      return role;

  public void setRole(String role) {
      this.role = role;

This is also important to understand that if above validation fails then later phases (like 'update model values') are skipped and 'render response' phase is directly executed to show the error message.

Creating converter and validator tags


<?xml version="1.0" encoding="UTF-8"?>

Registering the tags


<web-app xmlns=""
                   " version="3.1">

The param javax.faces.VALIDATE_EMPTY_FIELDS is false by default, i.e. validator is not invoked for empty or null input. As our converter returns null for invalid input, it is necessary to invoke validator for that scenario as well. So we are setting this parameter to true.

JSF page


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns=""
    <h2>JSF custom converter and Validator example</h2>
        <h:panelGrid columns="3">

            <h:outputLabel for="mgr" value="Manager Id: "/>
            <h:inputText id="mgr" value="#{employeeBean.manager}">
                <u:employeeConverter by="id" />
                <u:employeeValidator role="manager"/>
            <h:message for="mgr" style="color:red"/>

            <h:outputLabel for="dev" value="Developer Name: "/>
            <h:inputText id="dev" value="#{employeeBean.developer}">
                <u:employeeConverter by="name" />
                <u:employeeValidator role="developer"/>
            <h:message for="dev" style="color:red"/>

            <h:commandButton value="Submit"/>
            <h:outputText value="#{employeeBean.manager}"/>
            <h:outputText value="#{employeeBean.developer}"/>

Managed Bean

public class EmployeeBean {
  private Employee manager;
  private Employee developer;

  public Employee getManager() {
      return manager;

  public void setManager(Employee manager) {
      this.manager = manager;

  public Employee getDeveloper() {
      return developer;

  public void setDeveloper(Employee developer) {
      this.developer = developer;

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war


Submitting invalid inputs:

In following screenshot, the first input is correct but second is not. Overall validation fails so model values are not updated.

Submitting valid inputs.

Example Project

Dependencies and Technologies Used:

  • jsf-api 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • jsf-impl 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • JDK 1.8
  • Maven 3.3.9

JSF Custom Converter and Validator Example Select All Download
  • custom-validator-and-converter-together
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • webapp
          • WEB-INF

See Also