Recently I was searching for an example about the arbitrary validation in JSF component, found very minimal examples in the web. Here I would like to share my knowledge on this.
One of the good feature in JSF is creating custom validator. To create a custom validator there two ways available.
a) Create a Validator class and register the class in faces-config.xml file. Then you can start using your custom validator wherever you want. You can find nice example here.
b) There are a few cases where you don’t see re usage of the validation logic, in that case obviously you will look for a simple solution. Fortunately JSF provides “Arbitrary Validation” feature to achieve this. Here are the steps.
1) Step1 – Bind a method expression to validator attribute of input component.
<h:inputText value=”#{userRegistrationBean.username}” required=”true”
validator=”#{userRegistrationBean.validateUsername}” />
2) Step2 – Write the specified validator method in the specified bean.
public class UserRegistrationBean {
public void validateUsername(FacesContext facesContext, UIComponent uiComponent,
Object value) throws ValidatorException {
// perform
if (the validation fails) {
throw new ValidatorException(new FacesMessage(“Invalid Username”));
}
}
}
Hope the above example helps you, let me know if you have further questions on this.

Leave a comment