org.springframework.binding.validation.support
Class HibernateRulesValidator

java.lang.Object
  extended by org.springframework.binding.validation.support.HibernateRulesValidator
All Implemented Interfaces:
RichValidator, Validator, ObjectNameResolver

public class HibernateRulesValidator
extends Object
implements RichValidator, ObjectNameResolver

Validator which uses the ClassValidator of Hibernate to discover InvalidValue. These are then translated to ValidationMessages and added to the ValidationResults as usual.

Usage in a ValidatingFormModel where SomeClass has annotations for Hibernate Validator:

 formModel.setValidator(new HibernateRulesValidator(formModel, SomeClass.class));
 

This can be used in combination with other Validators as well by creating a CompositeRichValidator:

 HibernateRulesValidator hibernateRulesValidator = new HibernateRulesValidator(getFormModel(), SomeClass.class);
 hibernateRulesValidator.addIgnoredHibernateProperty("ignoredProperty");
 RulesValidator rulesValidator = new RulesValidator(getFormModel(), myRulesSource);
 getFormModel().setValidator(new CompositeRichValidator(rulesValidator, hibernateRulesValidator));
 

Note that we're adding one property to the HibernateRulesValidator that will be ignored. This property will only be checked at your back-end by Hibernate. The RulesValidator adds additional rules that are only used at the front-end or/and may contain the equivalent of the AssertTrue or AssertFalse methods on SomeClass.

Author:
Andy DuPue, Lieven Doclo, Jan Hoskens

Constructor Summary
HibernateRulesValidator(ValidatingFormModel formModel, Class clazz)
          Creates a new HibernateRulesValidator without ignoring any properties.
HibernateRulesValidator(ValidatingFormModel formModel, Class clazz, Set<String> ignoredHibernateProperties)
          Creates a new HibernateRulesValidator with additionally a set of properties that should not be validated.
 
Method Summary
 void addIgnoredHibernateProperty(String propertyName)
          Add a property for the Hibernate validator to ignore.
protected  void addInvalidValues(org.hibernate.validator.InvalidValue[] invalidValues)
          Add all InvalidValues to the ValidationResults.
 void clearMessages()
          Clear the current validationMessages and the errors.
protected  org.hibernate.validator.InvalidValue[] doHibernateValidate(Object object, String property)
          Validates the object through Hibernate Validator
 void removeIgnoredHibernateProperty(String propertyName)
          Remove a property for the Hibernate validator to ignore.
 String resolveObjectName(String objectName)
          resolves a field name to a user friendly display name
protected  ValidationMessage translateMessage(org.hibernate.validator.InvalidValue invalidValue)
          Translate a single InvalidValue to a ValidationMessage.
 ValidationResults validate(Object object)
          Validates the provided object.
 ValidationResults validate(Object object, String propertyName)
          Validates the given property of the provided object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateRulesValidator

public HibernateRulesValidator(ValidatingFormModel formModel,
                               Class clazz)
Creates a new HibernateRulesValidator without ignoring any properties.

Parameters:
formModel - The ValidatingFormModel on which validation needs to occur
clazz - The class of the object this validator needs to check

HibernateRulesValidator

public HibernateRulesValidator(ValidatingFormModel formModel,
                               Class clazz,
                               Set<String> ignoredHibernateProperties)
Creates a new HibernateRulesValidator with additionally a set of properties that should not be validated.

Parameters:
formModel - The ValidatingFormModel on which validation needs to occur
clazz - The class of the object this validator needs to check
ignoredHibernateProperties - properties that should not be checked though are
Method Detail

validate

public ValidationResults validate(Object object)
Validates the provided object.

Specified by:
validate in interface Validator
Parameters:
object - the object to validate
Returns:
the results on the validation

validate

public ValidationResults validate(Object object,
                                  String propertyName)
Validates the given property of the provided object.

Specified by:
validate in interface RichValidator
Parameters:
object - the object to validate (may be an implementation of
propertyName - the name of the only property that has changed since the last call to validate.
Returns:
the results on the validation

addInvalidValues

protected void addInvalidValues(org.hibernate.validator.InvalidValue[] invalidValues)
Add all InvalidValues to the ValidationResults.


translateMessage

protected ValidationMessage translateMessage(org.hibernate.validator.InvalidValue invalidValue)
Translate a single InvalidValue to a ValidationMessage.


doHibernateValidate

protected org.hibernate.validator.InvalidValue[] doHibernateValidate(Object object,
                                                                     String property)
Validates the object through Hibernate Validator

Parameters:
object - The object that needs to be validated
property - The properties that needs to be validated
Returns:
An array of InvalidValue, containing all validation errors

clearMessages

public void clearMessages()
Clear the current validationMessages and the errors.

See Also:
validate(Object, String)

addIgnoredHibernateProperty

public void addIgnoredHibernateProperty(String propertyName)
Add a property for the Hibernate validator to ignore.

Parameters:
propertyName - Name of the property to ignore. Cannot be null.

removeIgnoredHibernateProperty

public void removeIgnoredHibernateProperty(String propertyName)
Remove a property for the Hibernate validator to ignore.

Parameters:
propertyName - Name of the property to be removed. Cannot be null.

resolveObjectName

public String resolveObjectName(String objectName)
resolves a field name to a user friendly display name

Specified by:
resolveObjectName in interface ObjectNameResolver


Copyright © 2004-2008 The Spring Framework. All Rights Reserved.