Tapestry IOC aware JSR-303 Custom Validators

DZone 's Guide to

Tapestry IOC aware JSR-303 Custom Validators

· Java Zone ·
Free Resource

In JSR-303, adding a custom field validator is a two step process.

  1. Create an annotation which will be placed on a field to be validated
  2. Create a validator which implements ConstraintValidator and link it to the above annotation

But there are situations where in you may need to access Tapestry services inside the validator. By default the validators are instantiated by ConstraintValidatorFactory which is by default provided by the ValidatorFactory. So to allow tapestry-ioc to resolve dependencies within the validators, we need to take instantiation into our own hands. Let us create a simple validator which will invoke a service to check if the name of an object is correct. (Stupid but simple)

Create an annotation

We start with an annotation

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Constraint(validatedBy = FooNameConstraintValidator.class)
public @interface FooNameConstraint {

public abstract String message() default "{com.googlecode.tawus.exports.FooNameConstraint.message}";

public abstract Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

All the three attributes are required and specify the message to be displayed, groups() to which this annotation is added to and payload() to assign custom payload objects(not used by API itself). Notice that we have linked the validator class using @Constraint attribute.

Create a validator

The validator is very simple.

public class FooNameConstraintValidator implements
ConstraintValidator<FooNameConstraint, String> {

private FooNameCheckService fooNameCheckService;

public FooNameConstraintValidator(FooNameCheckService fooNameCheckService){
this.fooNameCheckService = fooNameCheckService;

public void initialize(FooNameConstraint constraintAnnotation) {

public boolean isValid(String value, ConstraintValidatorContext context) {
return fooNameCheckService.isValid(value);


This validator calls FooNameCheckService to validate the name. As the validators are created only once, we can use only those services which have default scope. Per-thread services can be accessed by injecting ObjectLocator and the calling getService() method with the required Service interface as argument. FooNameCheckService is defined as

public interface FooNameCheckService {
boolean isValid(String Name);

public class FooNameCheckServiceImpl implements FooNameCheckService {

public boolean isValid(String name) {
//Check if the length is more than 5
return name != null && name.length() > 5;


Create Tapestry IOC Aware ConstraintValidatorFactory

Now, we have to take control of the instantiation of the validator. This can be done by providing our own implementation of ConstraintValidatorFactory.

public class TapestryConstraintValidatorFactory implements ConstraintValidatorFactory {

private ObjectLocator locator;

public TapestryConstraintValidatorFactory(ObjectLocator locator){
this.locator = locator;

public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
return locator.autobuild(key);


Contribute ConstraintValidatorFactory

Finally, we contribute this to BeanValidatorSource in the module class

public static void provideBeanValidatorConfigurer(
final ObjectLocator locator,
OrderedConfiguration<BeanValidatorConfigurer> configuration) {
configuration.add("constraint-validator", new BeanValidatorConfigurer() {
public void configure(javax.validation.Configuration<?> configuration) {
configuration.constraintValidatorFactory(new TapestryConstraintValidatorFactory(locator));

We also need to contribute this to hibernate-core, otherwise it will try to use its own implementation of ConstraintValidatorFactory. This can done by turning off the default validation by setting javax.persistence.validation.mode to none in hibernate.cfg.xml

<property name='javax.persistence.validation.mode'>none</property>

and then contributing an event-listener to the hibernate configuration


public static void contributeHibernateSessionSource(
final ObjectLocator locator,
OrderedConfiguration<HibernateConfigurer> config) {
config.add("hibernateConfiguration", new HibernateConfigurer() {
public void configure(org.hibernate.cfg.Configuration configuration) {
Configuration<?> validationConfig = Validation.byDefaultProvider()
.constraintValidatorFactory(new TapestryConstraintValidatorFactory(locator));
BeanValidationEventListener listener = new BeanValidationEventListener(
validationConfig.buildValidatorFactory(), configuration
configuration.setListener("pre-insert", listener);
configuration.setListener("pre-delete", listener);
configuration.setListener("pre-update", listener);



From http://tawus.wordpress.com/2011/05/12/tapestry-magic-12-tapestry-ioc-aware-jsr-303-custom-validators/


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}