Validating forms with RichFaces rich:beanValidator

The rich:beanValidator is a component designed to provide validation using Hibernate Validator model-based constraints (details about Hibernate Validator can be found at https://www.hibernate.org/412.html). In this recipe, we will validate a simple form made up of two fields representing the e-mail address and age of a user.

Getting ready

We developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library. In addition, we have used RichFaces 3.3.3.BETA1, which provides support for JSF 2.0. You can download this distribution from http://www.jboss.org/richfaces. The RichFaces libraries (including necessary dependencies) are in the book code bundle, under the /JSF_libs/RichFaces JSF 2.0 folder.

How to do it...

The rich:beanValidator tag is usually nested in a UI Component, like h:inputText. Next, you can see an example (notice that the summary attribute will contain details displayed about the validation error):

<h:form id="form">
<h:panelGrid columns="3">
<h:outputLabel for="email" value="Email Address:" />
<h:inputText id="email" value="#{bean.email}" label="Email">
<rich:beanValidator summary="Invalid Email address"/>
</h:inputText>
<rich:message for="email"/>
<h:outputLabel for="age" value="Age:" />
<h:inputText id="age" value="#{bean.age}" label="Age">
<rich:beanValidator
summary="Invalid age, must be between 18 and 90"/>
</h:inputText>
<rich:message for="age"/>
</h:panelGrid>
<h:commandButton value="Submit"></h:commandButton>
<rich:messages/>
</h:form>

The validator restrictions are specified in Hibernate style using the corresponding annotations in a bean. In our example, the Bean bean can be seen next:

package bean;
import org.hibernate.validator.Email;
import org.hibernate.validator.Range;
import org.hibernate.validator.NotEmpty;
public class Bean {
private String email;
private Integer age;
@Range(min=18, max=90)
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@NotEmpty
@Email
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

For more Hibernate validators check the org.hibernate.validator package. In our example, we have used the @Email, @NotEmpty, and @Range validators.

How it works...

It works like a common validator, but this time the validator restrictions are taken directly from the bean, instead of using dedicated attributes inside the validator tag.

There's more...

Another important validator from RichFaces is the rich:graphValidator. The rich:graphValidator component is much like rich:beanValidator. The difference between these two components is that in order to validate some input data with a rich:beanValidator component, it should be a nested element of an input component, whereas rich:graphValidator wraps multiple input components and validates the data received from them.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: Validate_forms_with_RichFaces_BeanValidator.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset