Hibernate validation using RichFaces

Important notice: Richfaces framework reached End of Life in 2016. Therefore, you cannot expect fixes or enhancements unless you fork the project and upgrade yourself the framework.
We recommend reading these guidelines, if you want to modernize your Richfaces application: How to migrate Richfaces Web applications to another Web UI

This is the second part tutorial about JSF validation. Now we will explore how to use Hibernate constraints to validate your JSF page using Richfaces and Seam framework.

Validation is a key part of every web application. Basically, when using a JSF application you can perform validation using two strategies:

In this article we will learn how to combine the Hibernate validator framework with Richfaces libraries. For the purpose of this article we are using RichFaces 3.3.3 which is both JSF 2, and JSF 1.2 compatible.

A detailed tutorial which shows how to install RichFaces 3.3.2 on JBoss 6 can be found here.

Hibernate constraints can be validated by RichFaces libraries by means of three tags:

<rich:ajaxValidator>

<rich:graphValidator>

<rich:beanValidator>

 <rich:ajaxValidator> can be used to validate a field using Ajax capabilities. This can be used for example if you want to validate your input fields meanwhile the user presses a key or when a field loses its focus. Here’s an example of it:

<h:form id="ajaxValidatorForm">
    <rich:panel>
        <f:facet name="header">
            <h:outputText value="User Info:" />
        </f:facet>
        <h:panelGrid columns="3">

            <h:outputText value="Username:" />
            <h:inputText value="#{bean.userName}" id="name" required="true">
                <rich:ajaxValidator event="onblur" />
            </h:inputText>
            <rich:message for="name" />

            <h:outputText value="Password:" />
            <h:inputText value="#{bean.password}" id="password">
                <rich:ajaxValidator event="onblur" />
            </h:inputText>
            <rich:message for="password" />

            <h:outputText value="Email:" />
            <h:inputText value="#{bean.email}" id="email">
                <rich:ajaxValidator event="onblur" />
            </h:inputText>
            <rich:message for="email" />

        </h:panelGrid>
    </rich:panel>

</h:form>

And this is the SampleBean class:

    
package sample;

import javax.faces.event.ActionEvent;
import org.hibernate.validator.*;

public class SampleBean {
@NotNull
@Size(min = 5, message = "Please enter the Email" )
@Email
private String email;

@NotNull
@Size(min = 5, max=20, message = "Please enter a valid username (5-20 characters)")
private String userName;

@NotNull
@Size(min = 5, max = 20, message = "Please enter a valid password (5-20 characters)")
private String password;

//. . . .Getter and setter methods

}    

If you lots of input fields in a form it can be tedious to add rich:ajaxValidator for all of them. Rather you can wrap ny number of components inside rich:graphValidator and rich:beanValidator will be automatically added to each input component.

<h:form id="ajaxValidatorForm2">
    <rich:graphValidator>
        <rich:panel>
            <f:facet name="header">
                <h:outputText value="User Info:" />
            </f:facet>
            <h:panelGrid columns="3">


                <h:outputText value="Username:" />
                <h:inputText value="#{bean.userName}" id="name"
                    required="true" />
                <rich:message for="name" />

                <h:outputText value="Password:" />
                <h:inputText value="#{bean.password}" id="password" />
                <rich:message for="password" />

                <h:outputText value="Email:" />
                <h:inputText value="#{bean.email}" id="email" />
                <rich:message for="email" />

            </h:panelGrid>
        </rich:panel>
    </rich:graphValidator>
</h:form>
Finally, you can use the

rich:beanValidator

 to validate the form fields when the user click the Submit button:
<h:form id="form">

<h:inputText id="age" value="#{bean.age}" label="Age">
<rich:beanValidator
summary="Invalid age, must be between 18 and 90"/>
</h:inputText>
. . . . . . .
<rich:messages/>
</h:form>

Beyond JSF with Seam Validation

Seam brings both Hibernate and JSF validations together to perform double checks on every data feld. It does this by first invoking UI Validations (JSF) and then annotation based Hibernate Validations.

Seam has introduced new tags <s:validate/> and <s:validateAll/> which invoke the Hibernate Validator for the input component. It registers a Hibernate Validator-JSF Validator bridge which helps in performing the Hibernate Validations from the UI to provide a feedback to the user. <s:validate> tag is required for each individual input component which we want to validate. If you want to validate all input fields in one shot, you can use the <s:validateAll> tag to invoke the validations on all the input fields. Lets see an example:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core" xmlns:s="http://jboss.org/seam/faces">
    <h:form>
        <div>
            <h:messages />
        </div>
        <s:validateAll>

            <div>
                Country:
                <h:inputText value="#{location.country}" />
            </div>

            <div>
                Zip code:
                <h:inputText value="#{location.zip}" />
            </div>

            <div>
                <h:commandButton />
            </div>

        </s:validateAll>

    </h:form>
</html>