Package org.apache.commons.validator
Class Validator
- java.lang.Object
-
- org.apache.commons.validator.Validator
-
- All Implemented Interfaces:
java.io.Serializable
public class Validator extends java.lang.Object implements java.io.SerializableValidations are processed by the validate method. An instance ofValidatorResourcesis used to define the validators (validation methods) and the validation rules for a JavaBean.- Version:
- $Revision: 1651910 $
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringBEAN_PARAMResources key the JavaBean is stored to perform validation on.protected java.lang.ClassLoaderclassLoaderThe class loader to use for instantiating application objects.static java.lang.StringFIELD_PARAMResources key theFieldis stored under.protected java.lang.StringfieldNameThe name of the field on the form to validatestatic java.lang.StringFORM_PARAMResources key theFormis stored under.protected java.lang.StringformNameThe name of the form to validatestatic java.lang.StringLOCALE_PARAMResources key theLocaleis stored.protected booleanonlyReturnErrorsSet this to true to not return Fields that pass validation.protected intpageThe current page number to validate.protected java.util.Map<java.lang.String,java.lang.Object>parametersMaps validation method parameter class names to the objects to be passed into the method.protected ValidatorResourcesresourcesThe Validator Resources.private static longserialVersionUIDprotected booleanuseContextClassLoaderWhether or not to use the Context ClassLoader when loading classes for instantiating new objects.static java.lang.StringVALIDATOR_ACTION_PARAMResources key theValidatorActionis stored under.static java.lang.StringVALIDATOR_PARAMResources key theValidatoris stored under.static java.lang.StringVALIDATOR_RESULTS_PARAMResources key theValidatorResultsis stored under.
-
Constructor Summary
Constructors Constructor Description Validator(ValidatorResources resources)Construct aValidatorthat will use theValidatorResourcespassed in to retrieve pluggable validators the different sets of validation rules.Validator(ValidatorResources resources, java.lang.String formName)Construct aValidatorthat will use theValidatorResourcespassed in to retrieve pluggable validators the different sets of validation rules.Validator(ValidatorResources resources, java.lang.String formName, java.lang.String fieldName)Construct aValidatorthat will use theValidatorResourcespassed in to retrieve pluggable validators the different sets of validation rules.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears the form name, resources that were added, and the page that was set (if any).java.lang.ClassLoadergetClassLoader()Return the class loader to be used for instantiating application objects when required.java.lang.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.java.lang.ObjectgetParameterValue(java.lang.String parameterClassName)Returns the value of the specified parameter that will be used during the processing of validations.booleangetUseContextClassLoader()Return the boolean as to whether the context classloader should be used.voidsetClassLoader(java.lang.ClassLoader classLoader)Set the class loader to be used for instantiating application objects when required.voidsetFieldName(java.lang.String fieldName)Sets the name of the field to validate in a form (optional)voidsetFormName(java.lang.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(java.lang.String parameterClassName, java.lang.Object parameterValue)Set a parameter of a pluggable validation method.voidsetUseContextClassLoader(boolean use)Determine whether to use the Context ClassLoader (the one found by callingThread.currentThread().getContextClassLoader()) to resolve/load classes that are defined in various rules.ValidatorResultsvalidate()Performs validations based on the configured resources.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
BEAN_PARAM
public static final java.lang.String BEAN_PARAM
Resources key the JavaBean is stored to perform validation on.- See Also:
- Constant Field Values
-
VALIDATOR_ACTION_PARAM
public static final java.lang.String VALIDATOR_ACTION_PARAM
Resources key theValidatorActionis stored under. This will be automatically passed into a validation method with the currentValidatorActionif it is specified in the method signature.- See Also:
- Constant Field Values
-
VALIDATOR_RESULTS_PARAM
public static final java.lang.String VALIDATOR_RESULTS_PARAM
Resources key theValidatorResultsis stored under. This will be automatically passed into a validation method with the currentValidatorResultsif it is specified in the method signature.- See Also:
- Constant Field Values
-
FORM_PARAM
public static final java.lang.String FORM_PARAM
Resources key theFormis stored under. This will be automatically passed into a validation method with the currentFormif it is specified in the method signature.- See Also:
- Constant Field Values
-
FIELD_PARAM
public static final java.lang.String FIELD_PARAM
Resources key theFieldis stored under. This will be automatically passed into a validation method with the currentFieldif it is specified in the method signature.- See Also:
- Constant Field Values
-
VALIDATOR_PARAM
public static final java.lang.String VALIDATOR_PARAM
Resources key theValidatoris stored under. This will be automatically passed into a validation method with the currentValidatorif it is specified in the method signature.- See Also:
- Constant Field Values
-
LOCALE_PARAM
public static final java.lang.String LOCALE_PARAM
Resources key theLocaleis stored. This will be used to retrieve the appropriateFormSetandFormto be processed.- See Also:
- Constant Field Values
-
resources
protected ValidatorResources resources
The Validator Resources.
-
formName
protected java.lang.String formName
The name of the form to validate
-
fieldName
protected java.lang.String fieldName
The name of the field on the form to validate- Since:
- 1.2.0
-
parameters
protected java.util.Map<java.lang.String,java.lang.Object> parameters
Maps validation method parameter class names to the objects to be passed into the method.
-
page
protected int page
The current page number to validate.
-
classLoader
protected transient java.lang.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 theuseContextClassLoadervariable.
-
useContextClassLoader
protected boolean useContextClassLoader
Whether or not to use the Context ClassLoader when loading classes for instantiating new objects. Default isfalse.
-
onlyReturnErrors
protected boolean onlyReturnErrors
Set this to true to not return Fields that pass validation. Only return failures.
-
-
Constructor Detail
-
Validator
public Validator(ValidatorResources resources)
Construct aValidatorthat will use theValidatorResourcespassed in to retrieve pluggable validators the different sets of validation rules.- Parameters:
resources-ValidatorResourcesto use during validation.
-
Validator
public Validator(ValidatorResources resources, java.lang.String formName)
Construct aValidatorthat will use theValidatorResourcespassed in to retrieve pluggable validators the different sets of validation rules.- Parameters:
resources-ValidatorResourcesto use during validation.formName- Key used for retrieving the set of validation rules.
-
Validator
public Validator(ValidatorResources resources, java.lang.String formName, java.lang.String fieldName)
Construct aValidatorthat will use theValidatorResourcespassed in to retrieve pluggable validators the different sets of validation rules.- Parameters:
resources-ValidatorResourcesto use during validation.formName- Key used for retrieving the set of validation rules.fieldName- Key used for retrieving the set of validation rules for a field- Since:
- 1.2.0
-
-
Method Detail
-
setParameter
public void setParameter(java.lang.String parameterClassName, java.lang.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.
-
getParameterValue
public java.lang.Object getParameterValue(java.lang.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.- Returns:
- value of the specified parameter.
-
getFormName
public java.lang.String getFormName()
Gets the form name which is the key to a set of validation rules.- Returns:
- the name of the form.
-
setFormName
public void setFormName(java.lang.String formName)
Sets the form name which is the key to a set of validation rules.- Parameters:
formName- the name of the form.
-
setFieldName
public void setFieldName(java.lang.String fieldName)
Sets the name of the field to validate in a form (optional)- Parameters:
fieldName- The name of the field in a form set- Since:
- 1.2.0
-
getPage
public int getPage()
Gets the page.This in conjunction with the page property of a
Fieldcan control the processing of fields. If the field's page is less than or equal to this page value, it will be processed.- Returns:
- the page number.
-
setPage
public void setPage(int page)
Sets the page.This in conjunction with the page property of a
Fieldcan control the processing of fields. If the field's page is less than or equal to this page value, it will be processed.- Parameters:
page- the page number.
-
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. TheValidatorResourceswill not be removed since it can be used again and is thread safe.
-
getUseContextClassLoader
public boolean getUseContextClassLoader()
Return the boolean as to whether the context classloader should be used.- Returns:
- whether the context classloader should be used.
-
setUseContextClassLoader
public void setUseContextClassLoader(boolean use)
Determine whether to use the Context ClassLoader (the one found by callingThread.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.
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
Return the class loader to be used for instantiating application objects when required. This is determined based upon the following rules:- The class loader set by
setClassLoader(), if any - The thread context class loader, if it exists and the
useContextClassLoaderproperty is set to true - The class loader used to load the Digester class itself.
- Returns:
- the class loader.
- The class loader set by
-
setClassLoader
public void setClassLoader(java.lang.ClassLoader classLoader)
Set the class loader to be used for instantiating application objects when required.- Parameters:
classLoader- The new class loader to use, ornullto revert to the standard rules
-
validate
public ValidatorResults validate() throws ValidatorException
Performs validations based on the configured resources.- Returns:
- The
Mapreturned uses the property of theFieldfor the key and the value is the number of error the field had. - Throws:
ValidatorException- If an error occurs during validation
-
getOnlyReturnErrors
public boolean getOnlyReturnErrors()
Returns true if the Validator is only returning Fields that fail validation.- Returns:
- whether only failed fields are returned.
-
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.- Parameters:
onlyReturnErrors- whether only failed fields are returned.
-
-