org.springmodules.validation.bean
Class RuleBasedValidator

java.lang.Object
  extended by org.springmodules.validation.bean.RuleBasedValidator
All Implemented Interfaces:
Validator
Direct Known Subclasses:
BeanValidator

public class RuleBasedValidator
extends Object
implements Validator

A Validator implementation which uses ValidationRule's to define its validation execution. There are two types of validation rules this validator accepts:

Author:
Uri Boness

Constructor Summary
RuleBasedValidator()
          Contrusts a new RuleBasedValidator for the given type.
 
Method Summary
 void addGlobalRule(Condition condition, String errorCode)
          Adds a new global validation rule to this validator.
 void addGlobalRule(Condition condition, String errorCode, Object[] args)
          Adds a new global validation rule to this validator.
 void addGlobalRule(Condition condition, String errorCode, String message)
          Adds a new global validation rule to this validator.
 void addGlobalRule(Condition condition, String errorCode, String message, Object[] args)
          Adds a new global validation rule to this validator.
 void addGlobalRule(ValidationRule globalRule)
          Adds the given validation rule as a global rule to this validator.
 void addPropertyGlobalRule(String propertyName, ValidationRule globalRule)
          Adds a property rule for the given property.
 void addPropertyRule(String propertyName, Condition fieldValueCondition, String errorCode)
          Adds the given property rule to this validator.
 void addPropertyRule(String propertyName, Condition fieldValueCondition, String errorCode, Object[] args)
          Adds the given property rule to this validator.
 void addPropertyRule(String propertyName, Condition fieldValueCondition, String errorCode, String message, Object[] args)
          Adds the given property rule to this validator.
 void addPropertyRule(String propertyName, ValidationRule propertyRule)
          Adds the given property rule for the given property.
 void setExtraGlobalVadlidationRules(ValidationRule[] globalRules)
          Sets extra global validation rules for this validator.
 void setExtraPropertyValidationRules(Map rulesByProperty)
          Sets extra property validation rules for this validator.
 boolean supports(Class clazz)
          This validator supports all classes.
 void validate(Object obj, Errors errors)
          Validates the given object and registers all validation errors with the given errors object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RuleBasedValidator

public RuleBasedValidator()
Contrusts a new RuleBasedValidator for the given type. After contruction, this validator will initially hold no rules.

Method Detail

supports

public boolean supports(Class clazz)
This validator supports all classes. Any object can be validated by this validator as long as the validation rules apply to it.

Specified by:
supports in interface Validator
See Also:
Validator.supports(Class)

validate

public void validate(Object obj,
                     Errors errors)
Validates the given object and registers all validation errors with the given errors object. The validation is done by applying all validation rules associated with this validator on the given object.

Specified by:
validate in interface Validator
See Also:
Validator.validate(Object, org.springframework.validation.Errors)

setExtraGlobalVadlidationRules

public void setExtraGlobalVadlidationRules(ValidationRule[] globalRules)
Sets extra global validation rules for this validator.

Parameters:
globalRules - The extra global validation rules to be added to this validator.

setExtraPropertyValidationRules

public void setExtraPropertyValidationRules(Map rulesByProperty)
Sets extra property validation rules for this validator.

Parameters:
rulesByProperty - The extra property validation rules for this validator. The map should hold the property names as keys and ValidationRule instances as values.

addPropertyRule

public void addPropertyRule(String propertyName,
                            Condition fieldValueCondition,
                            String errorCode)
Adds the given property rule to this validator. A property rule is a condition and error information that is associated with a property name. When this rule is applied on an object, the condition is checked against the value of the associated property of the (validated) object. All validation errors of this rule will be registered with the Errors object under the context of the associated property. Note that the associated property may be a nested property - in that case, the nested property value will be resolved and the condition will be applied on the this value.

Parameters:
propertyName - The name of the property the added rule is associated with.
fieldValueCondition - The condition of the rule.
errorCode - The error code of the rule.

addPropertyRule

public void addPropertyRule(String propertyName,
                            Condition fieldValueCondition,
                            String errorCode,
                            Object[] args)
Adds the given property rule to this validator.

Parameters:
propertyName - The name of the property the added rule is associated with.
fieldValueCondition - The condition of the rule.
errorCode - The error code of the rule.
args - The arguments of the error code of the rule.
See Also:
addPropertyRule(String, org.springmodules.validation.util.condition.Condition, String, Object[])

addPropertyRule

public void addPropertyRule(String propertyName,
                            Condition fieldValueCondition,
                            String errorCode,
                            String message,
                            Object[] args)
Adds the given property rule to this validator.

Parameters:
propertyName - The name of the property the added rule is associated with.
fieldValueCondition - The condition of the rule.
errorCode - The error code of the rule.
message - The default error message of the rule.
args - The arguments of the error code of the rule.
See Also:
addPropertyRule(String, org.springmodules.validation.util.condition.Condition, String, Object[])

addPropertyRule

public void addPropertyRule(String propertyName,
                            ValidationRule propertyRule)
Adds the given property rule for the given property.

Parameters:
propertyName - The name of the property associated with the added rule.
propertyRule - The rule that should be applied on the value of the given property.
See Also:
addPropertyRule(String, org.springmodules.validation.util.condition.Condition, String)

addPropertyGlobalRule

public void addPropertyGlobalRule(String propertyName,
                                  ValidationRule globalRule)
Adds a property rule for the given property. The given rule is actually a global rule, that is, it is being evaluated on the validated object, not on the property value. The only difference between this added rule and a global rule is that the validation errors of this rule are associated with the given property and are not associated globaly with the validated object.

This type of rule comes in handy when a complex validation is required on the validation object, but the error is should only be associated with a specific property. An example would be when two properties of the validated object should match (i.e. have the same value), but if they don't, the error will only be associated with one of them (e.g. when a UserRegistration object holds a password and confirmPassword properties - this should generally match, but if they don't, the error will only be associated with the confirmPassword property).

Parameters:
propertyName - The name of the property to associated with the added rule.
globalRule - The global rule to be added.

addGlobalRule

public void addGlobalRule(Condition condition,
                          String errorCode)
Adds a new global validation rule to this validator. The global validation rule applied on the object level and all validation error of this rule will be registered with the Errors object globaly (see Errors.reject(String)).

Parameters:
condition - The condition of the added rule.
errorCode - The error code of the added rule.

addGlobalRule

public void addGlobalRule(Condition condition,
                          String errorCode,
                          Object[] args)
Adds a new global validation rule to this validator.

Parameters:
condition - The condition of the added rule.
errorCode - The error code of the added rule.
args - The arguments for the error of the added rule.
See Also:
addGlobalRule(org.springmodules.validation.util.condition.Condition, String)

addGlobalRule

public void addGlobalRule(Condition condition,
                          String errorCode,
                          String message,
                          Object[] args)
Adds a new global validation rule to this validator.

Parameters:
condition - The condition of the added rule.
errorCode - The error code of the added rule.
message - The error message of the added rule.
args - The arguments for the error of the added rule.
See Also:
addGlobalRule(org.springmodules.validation.util.condition.Condition, String)

addGlobalRule

public void addGlobalRule(Condition condition,
                          String errorCode,
                          String message)
Adds a new global validation rule to this validator.

Parameters:
condition - The condition of the added rule.
errorCode - The error code of the added rule.
message - The default error message of the added rule.
See Also:
addGlobalRule(org.springmodules.validation.util.condition.Condition, String)

addGlobalRule

public void addGlobalRule(ValidationRule globalRule)
Adds the given validation rule as a global rule to this validator.

Parameters:
globalRule - The global rule to be added to this validator.


Copyright © 2009. All Rights Reserved.