パッケージ org.apache.commons.validator

Validator パッケージはXMLファイル(の定義)に基づく妥当性チェックを提供します。

参照:
          説明

クラスの概要
Arg デフォルトのアーギュメントまたは妥当性チェック定義で指定されたアーギュメント (例: required) はメッセージのパラメータに使用することができます。
Constant constant(このクラス) はグローバルまたはロケール毎に不変なフィールドを定義する際の置き換えに使用することができます。
Field フィールドに対して行う妥当性チェックとエラーメッセージの生成に使用する 入れ替え可能な妥当性チェックのリストとメッセージの情報と値を内部に持ちます。
Form form/JavaBean に対する妥当性チェックのルールを内部に持ちます。
FormSet Locale をベースにした国、言語等ごとに保持された Form のセットを保持します。
GenericTypeValidator このクラスは妥当性チェックの際に行われる基本的なクラス型変換メソッドを持ちます。
GenericValidator このクラスは妥当性チェックを行う基本的なメソッドを持ちます。
Msg 代換メッセージ(このクラス)は Field と関連付けられ ValidatorAction(入れ替え可能な妥当性チェック)に設定されたデフォルトメッセージの代わりに使用されます。
Validator 妥当性チェック処理を行います。
ValidatorAction 動的にインスタンス化され、実行される妥当性チェック処理の情報を保持します。
ValidatorResources Locale に関連した FormSet オブジェクトの保持を目的とするクラスです。
ValidatorResourcesInitializer XMLファイルの情報を ValidatorResources にマッピングします。
ValidatorResult このクラスは JavaBean に対して行われた妥当性チェック(のセット)の処理結果を保持します。
ValidatorResults このクラスは JavaBean に対して行われた妥当性チェック(のセット)の処理結果を保持します。
ValidatorUtil 基本的なユーティリティメソッド郡。
Var variable (このクラス)は入れ替え可能な妥当性チェックの情報を持つ Field と関連付けることができます。
 

例外の概要
ValidatorException Validator フレームワークの例外です。
 

パッケージ org.apache.commons.validator の説明

Validator パッケージはXMLファイル(の定義)に基づく妥当性チェックを提供します。

The Validator package provides validation for JavaBeans based on an xml file.


[依存する外部プロクト] [イントロダクション] [概要] [リソース] [使用例]
[Dependencies] [Introduction] [Overview] [Resources] [Usage Example]

依存する外部プロクト
External Dependencies

イントロダクション
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.

Validator を利用しようとする場合には、以下の基本的な手順を踏む必要があります:

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

概要
Overview

Commons Validator はXMLファイルに定義された妥当性チェックのルールを JavaBean に対して行う基本的な妥当性チェックのためのフレームワークです。 妥当性チェックが定義された Validator はXMLファイルによって定義することができます。 Validator の例では "required(必須)" のフィールドに対する妥当性チェックの際にどのクラスのどのメソッドを呼ぶかを定義できます。 妥当性チェックのルールはロケール毎、ルールの適用される JavaBean/Form 毎にグループ化することができます。 このフレームワークでは慣用的に使用されるいくつかのフィールド属性(チェックのルール)をサポートします。

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.

XMLファイルに定義され保持された妥当性チェックのルールは妥当性チェックの際に JavaBean に取り出されます。 Jakarta Commons BeanUtils (http://jakarta.apache.org/commons/beanutils.html) パッケージを使用し、階層構造をもったプロパティの参照をサポートします。 エラーメッセージとエラーメッセージ内の可変パラメータはフィールドの妥当性チェック処理と関連付けられます。

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

Validator インスタンスが生成された後、addResource メソッドを呼ぶことにより 妥当性チェックの処理を追加することができます。 以下に用意されたキー(とクラス名)を示します。

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

クラス名
Class Name
内部リソースキー
Validator Contant
内容
Description
java.lang.Object Validator.BEAN_KEY 妥当性チェックの対象となるJavaBean。
JavaBean that is being validated
java.util.Locale Validator.LOCALE_KEY FormSet を取得する際のロケール。 指定されない場合にはデフォルトのロケールが使用されます。
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_KEY 妥当性チェックを行う際に Validator のリソースから自動的に追加されます。 この名称がメソッドにて指定された場合、指定された ValidatorAction が妥当性チェックの処理に追加されます。
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_KEY 妥当性チェックを行う際に Validator のリソースから自動的に追加されます。 この名称がメソッドにて指定された場合、指定された Field が妥当性チェックの処理対象に追加されます。
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

これは required (必須) の妥当性チェックルールをBeanに対して行う基本的な例です。 この例はユニットテスト内で使用されています ( validator/src/test にある org.apache.commons.validator.RequiredNameTest と validator-name-required.xml を参照してください)。

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).

妥当性チェックのルールと処理を定義したXMLファイルを作成します。 必要となる妥当性チェックをXMLファイルに定義してください。

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の例
XML Example

"require(必須)" という入れ替え可能な妥当性チェックを定義します。

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>

"firstName" と "lastName" に "require(必須)" の妥当性チェックルールを追加します。

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

org.apache.commons.validator.RequiredNameTest から抜粋

Excerpts from org.apache.commons.validator.RequiredNameTest

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

// ValidatorResources インスタンスを
// XMLファイルにて初期化するために生成

// Create an instance of ValidatorResources to
// initialize from an xml file.

ValidatorResources resources = new ValidatorResources();

// ValidatorResources 内にリソースをロード
// Load resources into ValidatorResources passed in

ValidatorResourcesInitializer.initialize(resources, in);

// テスト対象となるBeanを生成

// Create bean to run test on.

Name name = new Name();

// ロードされたリソースとフォームキーから
// 妥当性チェック(Validator)インスタンスを生成
// Construct validator based on the loaded resources
// and the form key

Validator validator = new Validator(resources, "nameForm");
// 妥当性チェックを行うために
// 対象となるBeanを Validator に登録
// add the name bean to the validator as a resource
// for the validations to be performed on.

validator.addResource(Validator.BEAN_KEY, name);

// 妥当性チェックの結果を取得
// Get results of the validation.

Map hResults = null;

// ValidatorException が投げられるが
// この例では catch していない
// throws ValidatorException,
// but aren't catching for example

hResults = validator.validate();

if (hResults.get("firstName") == null) {
    // エラーなし
    // no error

} else {
    // "firstName" に対するエラーの数
    // number of errors for first name

    int errors = ((Integer)hResults.get("firstName")).intValue();
}

入れ替え可能な妥当性チェックの例
Pluggable Validator Example

'required' 入れ替え可能な妥当性チェック内に定義された妥当性チェックメソッド (org.apache.commons.validator.TestValidator から抜粋)。

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);
}

翻訳者:
日置 聡
翻訳状況:
初稿(校正者募集中)
翻訳更新日:
2003/07/28


このドキュメントは、Ja-Jakartaにより訳されました。 コメントがある場合は report@jajakarta.orgまでお願いします。
Translated into Japanese by jajakarta.org. The original page is here.
Copyright (c) 2002-2003 - Apache Software Foundation