6.3.1. < rich:ajaxValidator > available since 3.2.2

### Create new RichFaces Documentation Jira issue

Summary
255 characters left
Description
Environment
This will launch the RichFaces Jira page - to complete your feedback please login if needed, and submit the Jira.

JBoss.org Community Documentation

- Prev
- Next

### 6.3.1. < rich:ajaxValidator > available since 3.2.2

expand all

#### 6.3.1.1. Description

The <rich:ajaxValidator> is a component designed to provide Ajax validation inside for JSF inputs.

#### 6.3.1.2. Key Features

- Skips all JSF processing except validation

- Possibility to use both standard and custom validation

- Possibility to use Hibernate Validation

- Event based validation triggering

#### 6.3.1.3. Details of Usage

The <rich:ajaxValidator> component should be added as a child component to an input JSF tag which data should be validated and an event that triggers validation should be specified as well. The component is ajaxSingle by default so only the current field will be validated.

The following example demonstrates how the <rich:ajaxValidator> adds Ajax functionality to standard JSF validators. The request is sent when the input field loses focus, the action is determined by the "event" attribute that is set to `"onblur"`.

    ...

    <rich:panel>

          <f:facet name="header">

                <h:outputText value="User Info:" />

          </f:facet>

          <h:panelGrid columns="3">

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

                <h:inputText value="#{userBean.name}" id="name" required="true">

                      <f:validateLength minimum="3" maximum="12"/>

                      <rich:ajaxValidator event="onblur"/>

                </h:inputText>

                <rich:message for="name" />

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

                      <h:inputText value="#{userBean.age}" id="age" required="true">

                            <f:convertNumber integerOnly="true"/>

                            <f:validateLongRange minimum="18" maximum="99"/>

                            <rich:ajaxValidator event="onblur"/>

                      </h:inputText>

                      <rich:message for="age"/>

          </h:panelGrid>

    </rich:panel>

    ...

This is the result of the snippet.

Figure 6.7. Simple example of <rich:ajaxValidator> with

In the example above it's show how to work with standard JSF validators. The <rich:ajaxValidator> component also works perfectly with custom validators enhancing their usage with Ajax.

Custom validation can be performed in two ways:

- Using JSF Validation API is available in javax.faces.validator package

- Using Hibernate Validator, specifying a constraint for the data to be validated. A reference on Hibernate Validator can be found in Hibernated documentation.

The following example shows how the data entered by user can be validated using Hibernate Validator.

    ...

    <rich:panel>

          <f:facet name="header">

                <h:outputText value="User Info:" />

          </f:facet>

          <h:panelGrid  columns="3">

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

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

                      <rich:ajaxValidator event="onblur" />

                </h:inputText>

                <rich:message for="name" />

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

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

                            <rich:ajaxValidator event="onblur" />

                      </h:inputText>

                      <rich:message for="email" />

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

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

                            <rich:ajaxValidator event="onblur" />

                      </h:inputText>

                      <rich:message for="age" />

          </h:panelGrid>

    </rich:panel>

    ...

Here is the source code of the managed bean.

    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;

    import org.hibernate.validator.Pattern;

    public class ValidationBean {

        private String progressString="Fill the form please";

        @NotEmpty

        @Pattern(regex=".*[^\\s].*", message="This string contain only spaces")

        @Length(min=3,max=12)

        private String name;

        @Email

        @NotEmpty

        private String email;

        @NotNull

        @Min(18)

        @Max(100)

        private Integer age;

        public ValidationBean() {

        }

        /* Corresponding Getters and Setters */

    }

By default the Hibernate Validator generates an error message in 10 language, though you can redefine the messages that are displayed to a user when validation fails. In the shows example it was done by adding `(message="wrong email format")` to the `@Email` annotation.

This is how it looks.

Figure 6.8. Validation using Hibernate validator

#### 6.3.1.4. Reference Data

Table of <rich:ajaxValidator> attributes.

Table 6.19. Component Identification Parameters

Name | Value
--- | ---
component-type | org.richfaces.ajaxValidator
component-class | org.richfaces.component.html.HtmlajaxValidator
component-family | org.richfaces.ajaxValidator
renderer-type | org.richfaces.ajaxValidatorRenderer
tag-class | org.richfaces.taglib.ajaxValidatorTag

#### 6.3.1.5. Relevant Resources Links

Visit the AjaxValidator page at RichFaces LiveDemo for examples of component usage and their sources.

- Prev6.3. Ajax Validators
- Top of page
- Front page
- Next6.3.2. < rich:beanValidator > ...