Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Requires Platform 4.5+

Validating Client Input Data

In Nexaweb 4.5, you can declaratively create logic to validate data that users input to fields on the client user interface.

...

クライアント入力データの検証

Nexaweb 4.5 では、ユーザーがクライアントユーザーインターフェイスのフィールドに入力したデータを検証するロジックを宣言的に作成できます。

この機能は、Nexaweb 4.5 では、バリデータプラグインフレームワークの com.nexaweb.plugin.validation.validators.Validator

...

The validator plug-in framework provides the following validator tags, in addition to the ability for you to extend the Validator class by creating your own tags: 

...

Validates whether a field contains data of the specified length.

...

Validates whether a field contains valid whole numbers (integers). This validates based on amount of data entered.

...

Validates whether a field contains decimal numbers.

...

Validates whether a field contains the specified range of integers or decimal numbers. This validates based on actual values of data entered.

...

Validates whether a field contains a date in the specified format.

...

Validates whether a field contains decimal numbers and contains the specified prefix (for currency type).

In addition to the supplied validators, you can also create:

...

Allows you to refer to an existing validator without specifying the entire validator declaration. Use validator references to refer to a validator in an validator chain.

 

 Using Validators

This section provides an overview of how to use validators in your client application's UI.

Using validators requires the following major steps:

  • Declare validator
  • Define element to validate
  • Create and specify the onSuccess MCO to run
  • Create and specify the onFailure MCO to run
  • Invoke validator on event in UI File 

...

Declare each validator outside the rootpane of the application UI file.

To use the same validator type for multiple fields in a UI file, declare unique validators for each field to provide a unique message to the user through the OnSuccess and OnFailure event for each field.

...

Determine the element in your UI that you want to validate and define it in the value attribute of the validator.

...

Create an MCO or Macro that executes whatever task you want the UI to perform upon successful validation, for example to display a success message.

...

Create an MCO or Macro that executes whatever task you want the UI to perform upon failed validation, for example to display a failed message.

...

In the rootpane of the UI file, specify the command to invoke a declared validator on an event for the field to validate.

...

クラスを通じて提供されます。

バリデータプラグインフレームワークは次のバリデータタグを提供します。また、独自のタグを作成して Validator クラスを拡張することも可能です。
 
バリデータ
説明
requiredFieldValidator
フィールドにデータが含まれているかどうかを検証します。
stringLengthValidator
フィールドに指定した長さのデータが含まれているかどうかを検証します。
integerValidator
フィールドに有効な整数が含まれているかどうかを検証します。この検証は、入力されたデータの量に基づいて行われます。
decimalValidator
フィールドに 10 進数が含まれているかどうかを検証します。
rangeValidator
フィールドに指定した範囲の整数または 10 進数が含まれているかどうかを検証します。この検証は、入力されたデータの実際の値に基づいて行われます。
dateValidator
フィールドに指定したフォーマットの日付が含まれているかどうかを検証します。
currencyValidator
フィールドに 10 進数と指定したプレフィックス (通貨型用) が含まれているかどうかを検証します。

用意されているバリデータに加えて、次のものを作成することもできます。
バリデータ要素
説明
バリデータ チェーン
UI ファイルの同じフィールドまたは異なるフィールドで実行する一連のバリデータを定義します。
バリデータ参照
バリデータ宣言全体を指定せずに、既存のバリデータを参照できるようにします。バリデータ チェーン内のバリデータを参照するには、バリデータ参照を使用します。

バリデータの使用

ここでは、クライアント アプリケーションの UI でバリデータを使用する方法の概要を示します。

バリデータの使用に必要となる主な手順は、次のとおりです。
  • バリデータを宣言する
  • 検証する要素を定義する
  • 実行する onSuccess MCO を作成および指定する
  • 実行する onFailure MCO を作成および指定する
  • UI ファイルのイベントでバリデータを呼び出す
手順
説明
バリデータを宣言する
アプリケーション UI ファイルの rootPane の外部で、各バリデータを宣言します。
UI ファイルの複数のフィールドで同じバリデータ タイプを使用するには、フィールドごとに一意のバリデータを宣言し、各フィールドの OnSuccess イベントおよび OnFailureイベントを通じて一意のメッセージをユーザーに提供します。
検証する要素を定義する
UI 内の検証する要素を決定し、その要素をバリデータの value 属性で定義します。
実行する onSuccess イベントを作成および指定する
検証が成功したときに UI で実行するタスク (たとえば、成功のメッセージを表示する) を実行する MCO またはマクロを作成します。
実行する onFailure イベントを作成および指定する
検証が失敗したときに UI で実行するタスク (たとえば、失敗のメッセージを表示する) を実行する MCO またはマクロを作成します。
UI ファイルのイベントでバリデータを呼び出す
UI ファイルの rootPane で、検証するフィールドのイベントで宣言済みのバリデータを呼び出すコマンドを指定します。
たとえば、onTextChange=“validator://balanceValidator.execute()”
.
と指定します。
 

Declaring Validators

You declare validators outside the rootpane of a UI file using the following basic structure:

...


バリデータの宣言
バリデータは、UI ファイルの rootPane の外部で、次の基本構造を使用して宣言します。

 

Code Block
<validator:stringLengthValidator
xmlns=

...

“http://nexaweb.com/

...

validator”

...

id=

...

“passwordValidator”
value=

...

“password.

...

text”

...

onSuccess=

...

“mco://myMco.validPassword()

...


...

onFailure=

...

“mco://myMco.invalidPassword()

...

 />
Validator Tag ElementDescription
バリデータ タグ要素
説明
id=“passwordValidator”
Specifies a unique ID for this instance of this type of validator.
特定のバリデータ タイプの特定のインスタンスに一意の ID を指定します。
value=“password.text”
Specifies the
ID of a textField or passwordField in the UI file and the text attribute of the field to validate.
UI ファイルの textField または passwordField の ID と、そのフィールドの検証対象の text属性を指定します。
onSuccess=
“mco
Specifies the MCO or Macro to run - for example, to display a success message - upon successful validation.
検証が成功したときに実行する MCO またはマクロ (たとえば、成功のメッセージを表示する) を指定します。
onFailure=
“mco

Specifies the MCO or Macro to run - for example, to display a failed message - upon unsuccessful validation.

Note: See Supplied Validators for a complete list of all attributes available for each validator.
 

Define Element to Validate

As validators provide a means for your UI to validate user input, you primarily invoke validators on textField or passwordField elements.

To define elements to validate, you must specify the following attributes for the element to validate:

textField AttributeDescription
検証が成功しなかったときに実行する MCO またはマクロ (たとえば、失敗のメッセージを表示する) を指定します。
 

検証する要素の定義

バリデータは UI でユーザー入力を検証する手段を提供するものなので、主に textField 要素または passwrodField 要素で呼び出します。

検証する要素を定義するには、検証する要素の次の属性を指定する必要があります。
textField 属性
説明
id=" "
Specifies a unique ID for this textField element.
特定の textField 要素に一意の ID を指定します。
text=" "

Specifies the attribute by which the Nexaweb client reads user-supplied text.

...

Nexaweb クライアントで、ユーザーが入力したテキストを読み込むための属性を指定します。

:

<textField

...

id=

...

“startDate” text=

...

“” />

...


In addition to specifying these textField element attributes, you can also specify in your textField element tag the textField event on which to invoke the validator. See the section in this document on Invoking Validators.

To complete the element definition, you must identify this textField in the value attribute of the declaration of the validator you want to invoke. See the Declaring Validators section of this document. 

Create and Specify the onSuccess and onFailure Events to Run

If you wish to display a message or some feedback to the user to reflect the success or failure of the validation of an element, you must peform the following major steps:

  • Create an MCO or Macro to handle this event
  • Declare the MCO or Marco in the UI file that includes the validator
  • Invoke the MCO or Marco from the validator's onSuccess and/or onFailure attributes

...


これらの textField 要素属性を指定することに加えて、バリデータを呼び出す textField イベントを textField 要素タグで指定することもできます。このドキュメントの「バリデータの呼び出し」を参照してください。
要素の定義を完成させるには、呼び出すバリデータの定義の value 属性で、この textField を指定する必要があります。このドキュメントの「バリデータの宣言」を参照してください。
 

実行する onSuccess イベントおよび onFailure イベントの作成と指定

要素の検証が成功または失敗したことを伝えるメッセージや何らかのフィードバックをユーザーに表示する場合に、実行する必要のある主な手順は次のとおりです。
  • このイベントを処理する MCO またはマクロを作成する
  • バリデータを含む UI ファイルで MCO またはマクロを宣言する
  • バリデータの onSuccess 属性または onFailure属性 (あるいはその両方) から MCO またはマクロを呼び出す
:
Code Block
languagehtml/xml
linenumberstrue
<mco:declarations xmlns:mco="http://nexaweb.com/mco">
    <mco:mco id="loginMco" src="mcos.LoginMco"
  </mco:declarations>
  <nxml xmlns:validator="http://nexaweb.com/validator">
    <validator:requiredFieldValidator id="usernameValidator" 
     value="username.text" onSuccess="" 
     onFailure="macro://failureMacro.execute('Username required')" />
    <validator:requiredFieldValidator id="passwordValidator" 
     value="password.text" onSuccess="mco://loginMco.enableLogInButton()" 
     onFailure="macro://failureMacro.execute('Password required')" />
  </nxml>
  <dialog id="loginDialog" title="Login" modal="true" closable="false">
    <flowLayout />
    <label text="Username:" />
    <textField id="username" text="" focused="true" 
     onTextChange="validator://usernameValidator.execute()" />
    <label text="Password:" />
    <passwordField id="password" text="" 
     onTextChange="validator://passwordValidator.execute()" />
    <button text="Log In" enabled="false" onCommand="mco://loginMco.login()" />
  </dialog> 

...


UI

...

ファイルのイベントでのバリデータの呼び出し

バリデータを呼び出すには、要素イベント ハンドラの値として次のコマンドを指定します。

...

where usernameValidator specifies the name of the defined validator tag to invoke.

You can invoke a validator on any valid event handler for the textField or passwordField elements.

For example:


usernameValidator には、呼び出す定義済みバリデータタグの名前を指定します。

バリデータは、textField 要素または passwordField 要素の任意の有効なイベント ハンドラで呼び出すことができます。

例:

 

Code Block
<dialog>
  <textField id="username" text="" />
  <passwordField id="password" text="" />
  <button text="Login" onCommand="validator://loginValidator.execute()"/>
</dialog> 

Putting It All Together

...

まとめ


次の例では、UI ファイルでバリデータを使用する方法を注釈付きで示します。

 

Code Block
linenumbers
languagehtml/xmltrue
 <!-- 1 --> 
<mco:declarations xmlns:mco="http://nexaweb.com/mco">
  <mco:mco id="loginMco" src="mcos.LoginMco">
</mco:declarations>

<!-- 2 --> 
<nxml xmlns:validator="http://nexaweb.com/validator">
  <validator:requiredFieldValidator <!-- 3--> id="usernameValidator" 
<!-- 4--> value="username.text" onSuccess=""
<!-- 5 --> onFailure="macro://failureMacro.execute('Username required')"/>
  validator:requiredFieldValidator id="passwordValidator" 
  value="password.text" onSuccess="mco://loginMco.enableLogInButton()" 
  onFailure="macro://failureMacro.execute('Password required')" />
</nxml>

<dialog id="loginDialog" title="Login" modal="true" closable="false">
  <flowLayout />
  <label text="Username:" />
  <textField <!-- 6-->id="username" text="" focused="true" 
<!-- 7 --> onTextChange="validator://usernameValidator.execute()" />
  <label text="Password:" />
  <passwordField id="password" text="" onTextChange="validator://passwordValidator.execute()" />
  <button text="Log In" enabled="false" onCommand="mco://loginMco.login()" />
</dialog> 
Step
手順
Description
説明
1
Declares
MCO to handle validator onSuccess event to display message to user on successful validation of login.
ログインの検証に成功したときにユーザーにメッセージを表示するバリデータの onSuccessイベントを処理する MCO を宣言します。
2

Declares validators in UI File. This example declares two required field validators, one for the textField with an ID of username, and one for the passwordField with ID of password.

3Specifies the unique ID of the UI element to validate.
4The value validator attribute specifies the ID and text attributes of the element  to validate.
5Specifies the onFailure event to run for this validator. The onFailure event is defined by the loginMco MCO.
6Specifies a unique ID for the UI element that you want to validate.
7Specifies the UI element event that invokes the validator.
UI ファイルでバリデータを宣言します。この例では、2 つの必須フィールド バリデータを宣言します。1 つは username という ID の textField 用、もう 1 つは password という ID のpasswordField 用です。
3
検証する UI 要素の一意の ID を指定します。
4
valueバリデータ属性は、検証する要素の ID属性と text 属性を指定します。
5
このバリデータで実行する onFailureイベントを指定します。この onFailureイベントは、loginMcoという MCO によって定義されます。
6
検証する UI 要素の一意の ID を指定します。
7
バリデータを呼び出す UI 要素イベントを指定します。