JBoss.org Community Documentation

- Prev
- Next

## 6.26. < rich:beanValidator >

### 6.26.1. Description

The <rich:beanValidator> is a component designed to provide validation using Hibernate model-based constraints

### 6.26.2. Key Features

- Validation using Hibernate constraints

Table 6.51. rich : beanValidator attributes

Attribute Name | Description
--- | ---
binding | binding
summary | Summary message for a validation errors.

Table 6.52. Component identification parameters

Name | Value
--- | ---
component-type | org.richfaces.beanValidator
component-class | org.richfaces.component.html.HtmlbeanValidator
component-family | org.richfaces.beanValidator
renderer-type | org.richfaces.beanValidatorRenderer
tag-class | org.richfaces.taglib.beanValidatorTag

### 6.26.3. Creating the Component with a Page Tag

To create the simplest variant of the component on a page use the following syntax:

Example:

    ...

    <h:inputText value="#{validationBean.email}" id="email">

    <rich:beanValidator summary="Invalid email"/>

    </h:inputText>

    ...

### 6.26.4. Creating the Component Dynamically Using Java

Example:

    import org.richfaces.component.html.HtmlCalendar;

    ...

    HtmlbeanValidator mybeanValidator= new HtmlbeanValidator();

    ...

#### 6.26.4.1. Details of Usage

Starting from 3.2.2 GA version Rich Faces provides support for model-based constraints defined using Hibernate Validator. Thus it's possible to use Hibernate Validators the same as for Seam based applications.

The <rich:beanValidator> component is defined in the same way as any JSF validator. Look at the example below.

    ...

          <rich:panel>

            <f:facet name="header">

              <h:outputText value="#{validationBean.progressString}" id="progress"/>

            </f:facet>

            <h:panelGrid columns="3">

              <h:outputText value="Name:" />

              <h:inputText value="#{validationBean.name}" id="name">

                <rich:beanValidator summary="Invalid name"/>

              </h:inputText>

              <rich:message for="name" />

              <h:outputText value="Email:" />

              <h:inputText value="#{validationBean.email}" id="email">

                <rich:beanValidator summary="Invalid email"/>

              </h:inputText>

              <rich:message for="email" />

              <h:outputText value="Age:" />

              <h:inputText value="#{validationBean.age}" id="age">

                <rich:beanValidator summary="Wrong age"/>

              </h:inputText>

              <rich:message for="age" />

              <f:facet name="footer">

                <a4j:commandButton value="Submit" action="#{validationBean.success}" reRender="progress"/>

              </f:facet>

            </h:panelGrid>

          </rich:panel>

          ...

Please play close attention on the bean code that contains the constraints defined with Hibernate annotation which perform validation of the input data.

    package org.richfaces.demo.validation;

    import org.hibernate.validator.Email;

    import org.hibernate.validator.Length;

    import org.hibernate.validator.Max;

    import org.hibernate.validator.Min;

    import org.hibernate.validator.NotEmpty;

    import org.hibernate.validator.NotNull;

    public class ValidationBean {

        @NotEmpty

        @Length(min=3,max=12)

        private String name;

        @Email

        @NotEmpty

        private String email;

        @NotNull

        @Min(18)

        @Max(100)

        private int age;

        public ValidationBean() {

        }

        public String getName() {

            return name;

        }

        public void setName(String name) {

            this.name = name;

        }

        public String getEmail() {

            return email;

        }

        public void setEmail(String email) {

            this.email = email;

        }

        public int getAge() {

            return age;

        }

        public void setAge(int age) {

            this.age = age;

        }

    }

The following figure shows what happens if validation fails

Figure 6.3. <rich:beanValidator> usage

As you can see from the example that in order to validate the <rich:beanValidator> should be nested into a input JSF or RichFaces component.

The component has the only attribute - "summary" which displays validation messages about validation errors.

- Prev6.25. < rich:graphValidator >
- Top of page
- Front page
- Next6.27. < rich:calendar >