Software Development

JSF – Managed Bean Custom Validation Methods


This post demonstrates how to create custom validation methods in the same @ManagedBean class and use them accordingly.


Stuff used in this post.

  • JSF 2.2
    • javax.faces-2.2.8.jar
  • JDK 8

Custom Validation Methods

These are the custom validation methods and their purposes are as follow for our example:

  • validateProductCode
    • A valid product code is required must start with 001-
  • validateProductName
    • A valid product name is required and must not exceed 100 characters.

These methods declare a particular set of parameters in a specific order so that they can be invoked by the JSF framework correctly.


Validation Method Parameters

A custom validation method can have any method name as long it follows these rules.

  1. The method must be public and non-static
  2. The method returns void
  3. The method has 3 parameters in a particular order declared in its method signature
    • public void validationAnyNameMethod(FacesContext context,
      UIComponent component, Object value)
  4. The method must declare that it throws ValidatorException (or not)

For instance,

Sample Codes

Managed bean – ProductWithCustomValidationForm


New Product Page – Request

This is our input page where users enter and save information for new 001- products.

Second Page – Response

This page displays the newly added product details.


With Validation Errors

Here we provided a product code that does not start with 001- and a product name that is too long.

Request Page



Without Validation Errors

This time we provided a product code that starts with 001- and a product name with valid number of characters.

Request Page


Using JSF Validator Interface instead

Each of these validation method can be converted into a class that implements the javax.faces.validator.Validator interface.

The reason why our validation methods have a specific set of parameters in a specific order is because JSF invokes them knowing that these methods are implementations of abstract method in javax.faces.validator.Validator.

We’ll cover that interface in part two of this post.





Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.

You Might Also Like