JBoss.org Community Documentation

- Prev
- Next

## 6.24. < rich:ajaxValidator >

### 6.24.1. Description

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

### 6.24.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

Table 6.47. rich : ajaxValidator attributes

Attribute Name | Description
--- | ---
ajaxListener | MethodExpression representing an action listener method that will be notified when this component is activated by the ajax Request and handle it. The expression must evaluate to a public method that takes an AjaxEvent parameter, with a return type of void
binding | The attribute takes a value-binding expression for a component property of a backing bean
data | Serialized (on default with JSON) data passed on the client by a developer on AJAX request. It's accessible via "data.foo" syntax
disableDefault | disableDefault
event | Name of JavaScript event property ( onclick, onchange, etc.) of parent component by which validation will be triggered
eventsQueue | Name of requests queue to avoid send next request before complete other from same event. Can be used to reduce number of requests of frequently events (key press, mouse move etc.)
focus | id of element to set focus after request completed on client side
id | Every component may have a unique id that is automatically created if omitted
ignoreDupResponses | Attribute allows to ignore an Ajax Response produced by a request if the newest 'similar' request is in a queue already. ignoreDupResponses="true" does not cancel the request while it is processed on the server, but just allows to avoid unnecessary updates on the client side if the response isn't actual now
limitToList | If "true", updates on client side ONLY elements from this 'reRender' property. If "false" (default) updates all rendered by ajax region components
onbeforedomupdate | JavaScript code for call before DOM has been updated on client side
oncomplete | JavaScript code for call after request completed on client side
onsubmit | JavaScript code for call before submission of ajax event
rendered | If "false", this component is not rendered
requestDelay | Attribute defines the time (in ms.) that the request will be wait in the queue before it is ready to send. When the delay time is over, the request will be sent to the server or removed if the newest 'similar' request is in a queue already
reRender | Id['s] (in format of call UIComponent.findComponent()) of components, rendered in case of AjaxRequest caused by this component. Can be single id, comma-separated list of Id's, or EL Expression with array or Collection
status | ID (in format of call UIComponent.findComponent()) of Request status component
summary | Summary message for a validation errors.
timeout | Response waiting time on a particular request. If a response is not received during this time, the request is aborted

Table 6.48. 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.24.3. Creating the Component with a Page Tag

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

Example:

    ...

    <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>

    ...

### 6.24.4. Creating the Component Dynamically Using Java

Example:

    import org.richfaces.component.html.HtmlCalendar;

    ...

    HtmlAjaxValidator myAjaxValidator= new HtmlAjaxValidator();

    ...

### 6.24.5. 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:panelGrid>

    </rich:panel>

    ...

This is the result of the snippet.

Figure 6.1. 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 here.

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:panelGrid>

    </rich:panel>

    ...

Here is the source code of the managed bean.

    ...

    package org.richfaces.demo.validation;

    import org.hibernate.validator.NotEmpty;

    import org.hibernate.validator.Email;

    import org.hibernate.validator.Length;

    public class ValidationBean {

        @NotEmpty

        @Length(min=3,max=12)

        private String name;

        @Email (message="wrong email format")

        @NotEmpty

        private String email;

        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;

        }

    }

    ...

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.2. Validation using Hibernate validator

- Prev6.23. < a4j:support >
- Top of page
- Front page
- Next6.25. < rich:graphValidator >