Package org.apache.commons.validator
Class Validator
java.lang.Object
org.apache.commons.validator.Validator
- All Implemented Interfaces:
- 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.- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringResources key the JavaBean is stored to perform validation on.protected ClassLoaderDeprecated.static final StringResources key theFieldis stored under.protected StringDeprecated.UsegetFieldName(), will be private in the next major version.static final StringResources key theFormis stored under.protected StringDeprecated.UsegetFormName(), will be private in the next major version.static final StringResources key theLocaleis stored.protected booleanDeprecated.UsegetOnlyReturnErrors(), will be private in the next major version.protected intDeprecated.UsegetPage(), will be private in the next major version.Deprecated.UsegetParameters(), will be private in the next major version.protected ValidatorResourcesDeprecated.UsegetResources(), will be private in the next major version.protected booleanDeprecated.UsegetUseContextClassLoader(), will be private in the next major version.static final StringResources key theValidatorActionis stored under.static final StringResources key theValidatoris stored under.static final StringResources key theValidatorResultsis stored under.
- 
Constructor SummaryConstructorsConstructorDescriptionValidator(ValidatorResources resources) Constructs aValidatorthat will use theValidatorResourcespassed in to retrieve pluggable validators the different sets of validation rules.Validator(ValidatorResources resources, String formName) Constructs aValidatorthat will use theValidatorResourcespassed in to retrieve pluggable validators the different sets of validation rules.Validator(ValidatorResources resources, String formName, String fieldName) Constructs aValidatorthat will use theValidatorResourcespassed in to retrieve pluggable validators the different sets of validation rules.
- 
Method SummaryModifier and TypeMethodDescriptionvoidclear()Clears the form name, resources that were added, and the page that was set (if any).Gets the class loader to be used for instantiating application objects when required.Gets the field name.Gets the form name which is the key to a set of validation rules.booleanReturns true if the Validator is only returning Fields that fail validation.intgetPage()Gets the page.Gets the parameter map.getParameterValue(String parameterClassName) Returns the value of the specified parameter that will be used during the processing of validations.Gets the validator resource.booleanGets the boolean as to whether the context classloader should be used.voidsetClassLoader(ClassLoader classLoader) Sets the class loader to be used for instantiating application objects when required.voidsetFieldName(String fieldName) Sets the name of the field to validate in a form (optional)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) Sets a parameter of a pluggable validation method.voidsetUseContextClassLoader(boolean useContextClassLoader) Sets whether to use the Context ClassLoader (the one found by callingThread.currentThread().getContextClassLoader()) to resolve/load classes that are defined in various rules.validate()Performs validations based on the configured resources.
- 
Field Details- 
BEAN_PARAMResources key the JavaBean is stored to perform validation on.- See Also:
 
- 
VALIDATOR_ACTION_PARAMResources 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:
 
- 
VALIDATOR_RESULTS_PARAMResources 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:
 
- 
FORM_PARAMResources 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:
 
- 
FIELD_PARAMResources 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:
 
- 
VALIDATOR_PARAMResources 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:
 
- 
LOCALE_PARAMResources key theLocaleis stored. This will be used to retrieve the appropriateFormSetandFormto be processed.- See Also:
 
- 
resourcesDeprecated.UsegetResources(), will be private in the next major version.The Validator Resources.
- 
formNameDeprecated.UsegetFormName(), will be private in the next major version.The name of the form to validate
- 
fieldNameDeprecated.UsegetFieldName(), will be private in the next major version.The name of the field on the form to validate- Since:
- 1.2.0
 
- 
parametersDeprecated.UsegetParameters(), will be private in the next major version.Maps validation method parameter class names to the objects to be passed into the method.
- 
pageDeprecated.UsegetPage(), will be private in the next major version.The current page number to validate.
- 
classLoaderDeprecated.UsegetClassLoader(), will be private in the next major version.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.
- 
useContextClassLoaderDeprecated.UsegetUseContextClassLoader(), will be private in the next major version.Whether or not to use the Context ClassLoader when loading classes for instantiating new objects. Default isfalse.
- 
onlyReturnErrorsDeprecated.UsegetOnlyReturnErrors(), will be private in the next major version.Sets this to true to not return Fields that pass validation. Only return failures.
 
- 
- 
Constructor Details- 
ValidatorConstructs aValidatorthat will use theValidatorResourcespassed in to retrieve pluggable validators the different sets of validation rules.- Parameters:
- resources-- ValidatorResourcesto use during validation.
 
- 
ValidatorConstructs 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.
 
- 
ValidatorConstructs 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 Details- 
clearClears 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.
- 
getClassLoaderGets 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 
- 
getFieldNameGets the field name.- Returns:
- the field name.
- Since:
- 1.10.0
 
- 
getFormNameGets the form name which is the key to a set of validation rules.- Returns:
- the name of the form.
 
- 
getOnlyReturnErrorsReturns true if the Validator is only returning Fields that fail validation.- Returns:
- whether only failed fields are returned.
 
- 
getPageGets 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.
 
- 
getParametersGets the parameter map.- Returns:
- the parameter map.
- Since:
- 1.10.0
 
- 
getParameterValueReturns 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.
 
- 
getResourcesGets the validator resource.- Returns:
- the validator resource.
- Since:
- 1.10.0
 
- 
getUseContextClassLoaderGets the boolean as to whether the context classloader should be used.- Returns:
- whether the context classloader should be used.
 
- 
setClassLoaderSets the class loader to be used for instantiating application objects when required.- Parameters:
- classLoader- The new class loader to use, or- nullto revert to the standard rules
 
- 
setFieldNameSets 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
 
- 
setFormNameSets the form name which is the key to a set of validation rules.- Parameters:
- formName- the name of the form.
 
- 
setOnlyReturnErrorsConfigures 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.
 
- 
setPageSets 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.
 
- 
setParameterSets 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.
 
- 
setUseContextClassLoaderSets 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:
- useContextClassLoader- determines whether to use Context ClassLoader.
 
- 
validatePerforms 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
 
 
- 
getClassLoader(), will be private in the next major version.