com.tzavellas.validation.spring
Class SpringValidator

java.lang.Object
  extended by com.tzavellas.validation.spring.SpringValidator
All Implemented Interfaces:
Validator

public class SpringValidator
extends Object
implements Validator

An adapter of the BeanValidator to the Spring's Validator interface.

This class is used to integrate the BeanValidator with the validation facilities offered by the Spring Framework and the Spring MVC.

Example code:

 import static com.tzavellas.validation.Validators.*;
 import com.tzavellas.validation.spring.SpringValidator;
 import org.springframework.validation.Validator;
 
 Validator validator = new SpringValidator(domain.Person.class,
          string("firstName").required(),
          string("lastName").required(),
          string("email").required().email(),
          integer("age").min(18),
          date("birthdate").past());
 

Thread safety: This class is thread safe.

Author:
Spiros Tzavellas
See Also:
BeanValidator, Validator

Constructor Summary
SpringValidator(Class<?> supportedClass)
          Create a SpringValidator that supports the specified class and an empty BeanValidator.
SpringValidator(Class<?> supportedClass, BeanValidator validator)
          Create a SpringValidator that supports the specified class and delegates to the specified BeanValidator.
SpringValidator(Class<?> supportedClass, PropertyValidator<?>... propertyValidators)
          Create a SpringValidator.
 
Method Summary
 void addValidator(PropertyValidator<?> v)
          Provide a PropertyValidator to be used by the wrapped BeanValidator.
protected  void doExtraValidation(Object target, Errors errors)
          This method can be overridden to provide custom validation logic in addition to the BeanValidator that this Validator delegates to.
 boolean isValid(Object target)
          A method to test if the specified object is valid.
 boolean supports(Class clazz)
          
 void validate(Object target, Errors errors)
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpringValidator

public SpringValidator(Class<?> supportedClass)
Create a SpringValidator that supports the specified class and an empty BeanValidator.

For an empty BeanValidator all objects are valid.


SpringValidator

public SpringValidator(Class<?> supportedClass,
                       PropertyValidator<?>... propertyValidators)
Create a SpringValidator.

Parameters:
supportedClass - the Class this validator supports
propertyValidators - the PropertyValidators to by used by the wrapped BeanValidator

SpringValidator

public SpringValidator(Class<?> supportedClass,
                       BeanValidator validator)
Create a SpringValidator that supports the specified class and delegates to the specified BeanValidator.

Method Detail

addValidator

public void addValidator(PropertyValidator<?> v)
Provide a PropertyValidator to be used by the wrapped BeanValidator.

Parameters:
v - the PropertyValidator

supports

public boolean supports(Class clazz)

Specified by:
supports in interface Validator

validate

public final void validate(Object target,
                           Errors errors)

Specified by:
validate in interface Validator

isValid

public boolean isValid(Object target)
A method to test if the specified object is valid.

Use this method if you don't care about the errors and the error messages and you just want to see if an object is valid.

Parameters:
target - the object to validate
Returns:
true if the specified object is valid, else false.

doExtraValidation

protected void doExtraValidation(Object target,
                                 Errors errors)
This method can be overridden to provide custom validation logic in addition to the BeanValidator that this Validator delegates to.

The default implementation is empty.

Parameters:
target - the object to validate
errors - can be used to report any validation errors


Copyright © 2007-2009 spiros.blog(). All Rights Reserved.