Creating a custom JSF validator
JSF provides an easy way to create custom validation logic to meet your business needs.
If this validation only occurs in a single page, then you may consider using validation inside the page-managed bean. However, if you want this validation to be used in different components or in different pages, consider creating a JSF validator.
In this recipe, we will create a custom validator for the employee's email
attribute that needs to start with the first letter of his first name. You can continue from the previous recipe, or you can grab this project's recipe by cloning the CreateCustomJSFValidator
application from the Git repository.
Getting ready
In order to create custom JSF validator, we need to perform the following steps:
Create a class that implements the
javax.faces.validattor.Validator
interface.Implement the
validate
method.Register this class as a validator inside the
faces-config.xml
file.Apply the validator on input components.
How to do it…
To know how...