org.apache.commons.validator

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons validator 1.3.1 src » org.apache.commons.validator 
org.apache.commons.validator
Package Documentation for org.apache.commons.validator The Validator package provides validation for JavaBeans based on an xml file.

Introduction

A common issue when receiving data either electronically or from user input is verifying the integrity of the data. This work is repetitive and becomes even more complicated when different sets of validation rules need to be applied to the same set of data based on locale for example. Error messages may also vary by locale. This package attempts to address some of these issues and speed development and maintenance of validation rules.

In order to use the Validator, the following basic steps are required:

  • Create a new instance of the org.apache.commons.validator.Validator class. Currently Validator instances may be safely reused if the current ValidatorResources are the same, as long as you have completed any previous validation, and you do not try to utilize a particular Validator instance from more than one thread at a time.
  • Add any resources needed to perform the validations. Such as the JavaBean to validate.
  • Call the validate method on org.apache.commons.validator.Validator.

Overview

The Commons Validator is a basic validation framework that lets you define validation rules for a JavaBean in an xml file. Validators, the validation definition, can also be defined in the xml file. An example of a validator would be defining what method and class will be called to perform the validation for a required field. Validation rules can be grouped together based on locale and a JavaBean/Form that the rules are associated with. The framework has basic support for user defined constants which can be used in some field attributes.

Validation rules can be defined in an xml file which keeps them abstracted from JavaBean you are validating. The property reference to a field supports nested properties using the Jakarta Commons BeanUtils (http://jakarta.apache.org/commons/beanutils.html) package. Error messages and the arguments for error messages can be associated with a fields validation.

Resources

After a Validator instance is created, instances of classes can be added to it to be passed into validation methods by calling the setParameter() method. Below is a list of reserved parameters (class names).

Class Name Validator Contstant Description
java.lang.Object Validator.BEAN_PARAM JavaBean that is being validated
java.util.Locale Validator.LOCALE_PARAM Locale to use when retrieving a FormSet. The default locale will be used if one isn't specified.
org.apache.commons.validator.ValidatorAction Validator.VALIDATOR_ACTION_PARAM This is automatically added to a Validator's resources as a validation is being processed. If this class name is used when defining a method signature for a pluggable validator, the current ValidatorAction will be passed into the validation method.
org.apache.commons.validator.Field Validator.FIELD_PARAM This is automatically added to a Validator's resources as a validation is being processed. If this class name is used when defining a method signature for a pluggable validator, the current Field will be passed into the validation method.

Usage Example

This is a basic example setting up a required validator for a name bean. This example is a working unit test (reference org.apache.commons.validator.RequiredNameTest and validator-name-required.xml located under validator/src/test).

Create an xml file with your validator and validation rules. Setup your required validator in your xml file.

XML Example
Validator Example
Pluggable Validator Example

XML Example

Definition of a 'required' pluggable validator.

<form-validation>
   <global>
       <validator name="required"
          classname="org.apache.commons.validator.TestValidator"
          method="validateRequired"
          methodParams="java.lang.Object, org.apache.commons.validator.Field"/>
    </global>
    <formset>
    </formset>
</form-validation>

Add validation rules to require a first name and a last name.

<form-validation>
   <global>
       <validator name="required"
          classname="org.apache.commons.validator.TestValidator"
          method="validateRequired"
          methodParams="java.lang.Object, org.apache.commons.validator.Field"/>
    </global>
    <formset>
       <form name="nameForm">
          <field property="firstName" depends="required">
             <arg0 key="nameForm.firstname.displayname"/>
          </field>
          <field property="lastName" depends="required">
             <arg0 key="nameForm.lastname.displayname"/>
          </field>
       </form>
    </formset>
</form-validation>

Validator Example

Excerpts from org.apache.commons.validator.RequiredNameTest

InputStream in = this.getClass().getResourceAsStream("validator-name-required.xml");

// Create an instance of ValidatorResources to
// initialize from an xml file.
ValidatorResources resources = new ValidatorResources(in);

// Create bean to run test on.
Name name = new Name();

// Construct validator based on the loaded resources
// and the form key
Validator validator = new Validator(resources, "nameForm");
// add the name bean to the validator as a resource
// for the validations to be performed on.
validator.setParameter(Validator.BEAN_PARAM, name);

// Get results of the validation.
Map results = null;

// throws ValidatorException,
// but aren't catching for example
results = validator.validate();

if (results.get("firstName") == null) {
    // no error
} else {
    // number of errors for first name     int errors = ((Integer)results.get("firstName")).intValue();
}

Pluggable Validator Example

Validation method defined in the 'required' pluggable validator (excerpt from org.apache.commons.validator.TestValidator).

public static boolean validateRequired(Object bean, Field field) {
    String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
    return GenericValidator.isBlankOrNull(value);
}

Java Source File NameTypeComment
Arg.javaClass

A default argument or an argument for a specific validator definition (ex: required) can be stored to pass into a message as parameters.

ByteTest.javaClass Performs Validation Test for byte validations.
CreditCardValidator.javaClass

Perform credit card validations.

By default, all supported card types are allowed.

CreditCardValidatorTest.javaClass Test the CreditCardValidator class.
CustomValidatorResourcesTest.javaClass Test custom ValidatorResources.
DateTest.javaClass Abstracts date unit tests methods.
DateValidator.javaClass

Perform date validations.

This class is a Singleton; you can retrieve the instance via the getInstance() method.

DoubleTest.javaClass Performs Validation Test for double validations.
EmailTest.javaClass Performs Validation Test for e-mail validations.
EmailValidator.javaClass

Perform email validations.

This class is a Singleton; you can retrieve the instance via the getInstance() method.

Based on a script by Sandeep V.

EntityImportTest.javaClass Tests entity imports.
ExceptionTest.javaClass Performs Validation Test for exception handling.
ExtensionTest.javaClass

Performs tests for extension in form definitions.

Field.javaClass This contains the list of pluggable validators to run on a field and any message information and variables to perform the validations and generate error messages.
FieldTest.javaClass Test Field objects.
FloatTest.javaClass Performs Validation Test for float validations.
Form.javaClass

This contains a set of validation rules for a form/JavaBean.

FormSet.javaClass Holds a set of Forms stored associated with a Locale based on the country, language, and variant specified.
FormSetFactory.javaClass Factory class used by Digester to create FormSet's.
GenericTypeValidator.javaClass This class contains basic methods for performing validations that return the correctly typed class based on the validation performed.
GenericValidator.javaClass This class contains basic methods for performing validations.
GenericValidatorTest.javaClass Test the GenericValidator class.
IntegerTest.javaClass Performs Validation Test for int validations.
ISBNValidator.javaClass A class for validating 10 digit ISBN codes.
ISBNValidatorTest.javaClass ISBNValidator Test Case.
LocaleTest.javaClass Performs Validation Test for locale validations.
LongTest.javaClass Performs Validation Test for long validations.
Msg.javaClass An alternative message can be associated with a Field and a pluggable validator instead of using the default message stored in the ValidatorAction (aka pluggable validator).
MultipleConfigFilesTest.javaClass Tests that validator rules split between 2 different XML files get merged properly.
MultipleTests.javaClass Performs Validation Test.
NameBean.javaClass Value object that contains a first name and last name.
ParameterTest.javaClass This TestCase is a confirmation of the parameter of the validator's method.
ParameterTestValidator.javaClass Contains validation methods for different unit tests.
RequiredIfTest.javaClass Performs Validation Test.
RequiredNameTest.javaClass Performs Validation Test.
RetrieveFormTest.javaClass Tests retrieving forms using different Locales.
ShortTest.javaClass Performs Validation Test for short validations.
TestCommon.javaClass Consolidates reading in XML config file into parent class.
TestNumber.javaClass Abstracts number unit tests methods.
TestPair.javaClass Groups tests and expected results.
TestTypeValidator.javaClass Contains validation methods for different unit tests.
TestValidator.javaClass Contains validation methods for different unit tests.
TypeBean.javaClass Value object that contains different fields to test type conversion validation.
TypeTest.javaClass Performs Validation Test for type validations.
UrlTest.javaClass Performs Validation Test for url validations.
UrlValidator.javaClass

Validates URLs.

Behavour of validation is modified by passing in options:
  • ALLOW_2_SLASHES - [FALSE] Allows double '/' characters in the path component.
  • NO_FRAGMENT- [FALSE] By default fragments are allowed, if this option is included then fragments are flagged as illegal.
  • ALLOW_ALL_SCHEMES - [FALSE] By default only http, https, and ftp are considered valid schemes.
  • Validator.javaClass Validations are processed by the validate method.
    ValidatorAction.javaClass Contains the information to dynamically create and run a validation method.
    ValidatorException.javaClass The base exception for the Validator Framework.
    ValidatorResources.javaClass

    General purpose class for storing FormSet objects based on their associated Locale.

    ValidatorResult.javaClass This contains the results of a set of validation rules processed on a JavaBean.
    ValidatorResults.javaClass This contains the results of a set of validation rules processed on a JavaBean.
    ValidatorResultsTest.javaClass Test ValidatorResults.
    ValidatorTest.javaClass Performs Validation Test.
    ValidatorTestSuite.javaClass Test suite for org.apache.commons.validator package.
    ValueBean.javaClass Value object for storing a value to run tests on.
    Var.javaClass A variable that can be associated with a Field for passing in information to a pluggable validator.
    VarTest.javaClass Test that the new Var attributes and the digester rule changes work.
    www.java2java.com | Contact Us
    Copyright 2009 - 12 Demo Source and Support. All rights reserved.
    All other trademarks are property of their respective owners.