org.apache.commons.validator

Class Validator

public class Validator extends Object implements Serializable

Validations are processed by the validate method. An instance of ValidatorResources is used to define the validators (validation methods) and the validation rules for a JavaBean.
Field Summary
static StringBEAN_KEY
Resources key the JavaBean is stored to perform validation on.
static StringBEAN_PARAM
Resources key the JavaBean is stored to perform validation on.
protected ClassLoaderclassLoader
The class loader to use for instantiating application objects.
protected StringformName
static StringFIELD_KEY
Resources key the Field is stored under.
static StringFIELD_PARAM
Resources key the Field is stored under.
protected HashMaphResources
protected static Loglog
Logger.
static StringLOCALE_KEY
Resources key the Locale is stored.
static StringLOCALE_PARAM
Resources key the Locale is stored.
protected booleanonlyReturnErrors
Set this to true to not return Fields that pass validation.
protected intpage
The current page number to validate.
protected Mapparameters
Maps validation method parameter class names to the objects to be passed into the method.
protected ValidatorResourcesresources
protected booleanuseContextClassLoader
Whether or not to use the Context ClassLoader when loading classes for instantiating new objects.
static StringVALIDATOR_ACTION_KEY
Resources key the ValidatorAction is stored under.
static StringVALIDATOR_ACTION_PARAM
Resources key the ValidatorAction is stored under.
static StringVALIDATOR_KEY
Resources key the Validator is stored under.
static StringVALIDATOR_PARAM
Resources key the Validator is stored under.
Constructor Summary
Validator(ValidatorResources resources)
Construct a Validator that will use the ValidatorResources passed in to retrieve pluggable validators the different sets of validation rules.
Validator(ValidatorResources resources, String formName)
Construct a Validator that will use the ValidatorResources passed in to retrieve pluggable validators the different sets of validation rules.
Method Summary
voidaddResource(String parameterClassName, Object parameterValue)
Add a resource to be used during the processing of validations.
voidclear()
Clears the form name, resources that were added, and the page that was set (if any).
ClassLoadergetClassLoader()
Return the class loader to be used for instantiating application objects when required.
StringgetFormName()
Gets the form name which is the key to a set of validation rules.
booleangetOnlyReturnErrors()
Returns true if the Validator is only returning Fields that fail validation.
intgetPage()
Gets the page.
ObjectgetParameterValue(String parameterClassName)
Returns the value of the specified parameter that will be used during the processing of validations.
ObjectgetResource(String parameterClassName)
Get a resource to be used during the processing of validations.
booleangetUseContextClassLoader()
Return the boolean as to whether the context classloader should be used.
voidsetClassLoader(ClassLoader classLoader)
Set the class loader to be used for instantiating application objects when required.
voidsetFormName(String formName)
Sets the form name which is the key to a set of validation rules.
voidsetOnlyReturnErrors(boolean onlyReturnErrors)
Configures which Fields the Validator returns from the validate() method.
voidsetPage(int page)
Sets the page.
voidsetParameter(String parameterClassName, Object parameterValue)
Set a parameter of a pluggable validation method.
voidsetUseContextClassLoader(boolean use)
Determine whether to use the Context ClassLoader (the one found by calling Thread.currentThread().getContextClassLoader()) to resolve/load classes that are defined in various rules.
ValidatorResultsvalidate()
Performs validations based on the configured resources.

Field Detail

BEAN_KEY

public static final String BEAN_KEY

Deprecated: Use BEAN_PARAM instead.

Resources key the JavaBean is stored to perform validation on.

BEAN_PARAM

public static final String BEAN_PARAM
Resources key the JavaBean is stored to perform validation on.

classLoader

protected ClassLoader classLoader
The class loader to use for instantiating application objects. If not specified, the context class loader, or the class loader used to load Digester itself, is used, based on the value of the useContextClassLoader variable.

formName

protected String formName

FIELD_KEY

public static final String FIELD_KEY

Deprecated: Use FIELD_PARAM instead.

Resources key the Field is stored under. This will be automatically passed into a validation method with the current Field if it is specified in the method signature.

FIELD_PARAM

public static final String FIELD_PARAM
Resources key the Field is stored under. This will be automatically passed into a validation method with the current Field if it is specified in the method signature.

hResources

protected HashMap hResources

Deprecated: Use parameters instead.

log

protected static Log log

Deprecated: Subclasses should use their own logging instance.

Logger.

LOCALE_KEY

public static final String LOCALE_KEY

Deprecated: Use LOCALE_PARAM instead.

Resources key the Locale is stored. This will be used to retrieve the appropriate FormSet and Form to be processed.

LOCALE_PARAM

public static final String LOCALE_PARAM
Resources key the Locale is stored. This will be used to retrieve the appropriate FormSet and Form to be processed.

onlyReturnErrors

protected boolean onlyReturnErrors
Set this to true to not return Fields that pass validation. Only return failures.

page

protected int page
The current page number to validate.

parameters

protected Map parameters
Maps validation method parameter class names to the objects to be passed into the method.

resources

protected ValidatorResources resources

useContextClassLoader

protected boolean useContextClassLoader
Whether or not to use the Context ClassLoader when loading classes for instantiating new objects. Default is false.

VALIDATOR_ACTION_KEY

public static final String VALIDATOR_ACTION_KEY

Deprecated: Use VALIDATOR_ACTION_PARAM instead.

Resources key the ValidatorAction is stored under. This will be automatically passed into a validation method with the current ValidatorAction if it is specified in the method signature.

VALIDATOR_ACTION_PARAM

public static final String VALIDATOR_ACTION_PARAM
Resources key the ValidatorAction is stored under. This will be automatically passed into a validation method with the current ValidatorAction if it is specified in the method signature.

VALIDATOR_KEY

public static final String VALIDATOR_KEY

Deprecated: Use VALIDATOR_PARAM instead.

Resources key the Validator is stored under. This will be automatically passed into a validation method with the current Validator if it is specified in the method signature.

VALIDATOR_PARAM

public static final String VALIDATOR_PARAM
Resources key the Validator is stored under. This will be automatically passed into a validation method with the current Validator if it is specified in the method signature.

Constructor Detail

Validator

public Validator(ValidatorResources resources)
Construct a Validator that will use the ValidatorResources passed in to retrieve pluggable validators the different sets of validation rules.

Parameters: resources ValidatorResources to use during validation.

Validator

public Validator(ValidatorResources resources, String formName)
Construct a Validator that will use the ValidatorResources passed in to retrieve pluggable validators the different sets of validation rules.

Parameters: resources ValidatorResources to use during validation. formName Key used for retrieving the set of validation rules.

Method Detail

addResource

public void addResource(String parameterClassName, Object parameterValue)

Deprecated: Use setParameter(String, Object) instead.

Add a resource to be used during the processing of validations.

Parameters: parameterClassName The full class name of the parameter of the validation method that corresponds to the value/instance passed in with it. parameterValue The instance that will be passed into the validation method.

clear

public void clear()
Clears the form name, resources that were added, and the page that was set (if any). This can be called to reinitialize the Validator instance so it can be reused. The form name (key to set of validation rules) and any resources needed, like the JavaBean being validated, will need to set and/or added to this instance again. The ValidatorResources will not be removed since it can be used again and is thread safe.

getClassLoader

public ClassLoader getClassLoader()
Return the class loader to be used for instantiating application objects when required. This is determined based upon the following rules:

getFormName

public String getFormName()
Gets the form name which is the key to a set of validation rules.

getOnlyReturnErrors

public boolean getOnlyReturnErrors()
Returns true if the Validator is only returning Fields that fail validation.

getPage

public int getPage()
Gets the page. This in conjunction with the page property of a Field can control the processing of fields. If the field's page is less than or equal to this page value, it will be processed.

getParameterValue

public Object getParameterValue(String parameterClassName)
Returns the value of the specified parameter that will be used during the processing of validations.

Parameters: parameterClassName The full class name of the parameter of the validation method that corresponds to the value/instance passed in with it.

getResource

public Object getResource(String parameterClassName)

Deprecated: Use getParameterValue(String) instead.

Get a resource to be used during the processing of validations.

Parameters: parameterClassName The full class name of the parameter of the validation method that corresponds to the value/instance passed in with it.

getUseContextClassLoader

public boolean getUseContextClassLoader()
Return the boolean as to whether the context classloader should be used.

setClassLoader

public void setClassLoader(ClassLoader classLoader)
Set the class loader to be used for instantiating application objects when required.

Parameters: classLoader The new class loader to use, or null to revert to the standard rules

setFormName

public void setFormName(String formName)
Sets the form name which is the key to a set of validation rules.

setOnlyReturnErrors

public void setOnlyReturnErrors(boolean onlyReturnErrors)
Configures which Fields the Validator returns from the validate() method. Set this to true to only return Fields that failed validation. By default, validate() returns all fields.

setPage

public void setPage(int page)
Sets the page. This in conjunction with the page property of a Field can control the processing of fields. If the field's page is less than or equal to this page value, it will be processed.

setParameter

public void setParameter(String parameterClassName, Object parameterValue)
Set a parameter of a pluggable validation method.

Parameters: parameterClassName The full class name of the parameter of the validation method that corresponds to the value/instance passed in with it. parameterValue The instance that will be passed into the validation method.

setUseContextClassLoader

public void setUseContextClassLoader(boolean use)
Determine whether to use the Context ClassLoader (the one found by calling Thread.currentThread().getContextClassLoader()) to resolve/load classes that are defined in various rules. If not using Context ClassLoader, then the class-loading defaults to using the calling-class' ClassLoader.

Parameters: use determines whether to use Context ClassLoader.

validate

public ValidatorResults validate()
Performs validations based on the configured resources.

Returns: The Map returned uses the property of the Field for the key and the value is the number of error the field had.

Copyright (c) 2001-2004 Apache Software Foundation