SeamFramework.orgCommunity Documentation

Seam Validation Module

Reference Guide

3.0.0.CR1

February 26, 2011


1. Introduction
2. Installation
2.1. Prerequisites
2.2. Maven setup
2.3. Manual setup
3. Dependency Injection
3.1. Retrieving of validator factory and validators via dependency injection
3.2. Dependency injection for constraint validators
4. Method Validation

The Seam Validation module provides CDI support for Hibernate Validator ...

This chapter describes the steps required to getting started with the Seam Validation Module.

The recommended way for setting up Seam Validation is using Apache Maven. The Seam Validation Module artifacts are deployed to the JBoss Maven repository. If not yet the case, therefore add this repository to your settings.xml file (typically in ~/.m2/settings.xml) in order to download the dependencies from there:


General information on the JBoss Maven repository is available in the JBoss community wiki, more information on Maven's settings.xml file can be found in the settings reference.

Having set up the repository you can add the Seam Validation Module as dependency to the pom.xml of your project. As most Seam modules the validation module is split into two parts, API and implementation. Generally you should be using only the types from the API within your application code. In order to avoid unintended imports from the implementation it is recommended to add the API as compile-time dependency, while the implementation should be added as runtime dependency only:


Note

Replace "x.y.z" in the properties block with the Seam Validation version you want to use.

The Seam Validation module provides enhanced support for dependency injection services related to bean validation. This support falls into two areas:

  • Retrieval of javax.validation.ValidatorFactory and javax.validation.Validator via dependency injection in non-Java EE environments

  • Dependency injection for constraint validators

As the Bean Validation API is part of Java EE 6 there is an out-of-the-box support for retrieving validator factories and validators instances via dependency injection in any Java EE 6 container.

The Seam Validation module provides the same service for non-Java EE environements such as for instance stand-alone web containers. Just annotate any field of type javax.validation.ValidatorFactory with @Inject to have the default validator factory injected:


It is also possible to directly inject a validator created by the default validator factory:


The Seam Validation module provides support for dependency injection within javax.validation.ConstraintValidator implementations. This is very useful if you need to access other CDI beans within you constraint validator such as business services etc.

To make use of dependency injection in constraint validators you have to configure org.jboss.seam.validation.InjectingConstraintValidatorFactory as the constraint validator factory to be used by the bean validation provider. To do so create the file META-INF/validation.xml with the following contents:


Having configured the constraint validator factory you can inject arbitrary CDI beans into you validator implementions. Listing Example 3.4, “Dependency injection within ConstraintValidator implementation” shows a ConstraintValidator implementation for the @Past constraint which uses an injected time service instead of relying on the JVM's current time to determine whether a given date is in the past or not.


The Seam Validation module provides ...