Skip to content

12. Example 09B – Model Validation

The following application is a variation of the previous one. Previously, for action A:

  • the validation rules file was named A-validation.xml
  • the validation error message file was named A.properties

Here, for action A of model M:

  • the validation rules file is named M-validation.xml
  • the validation error message file is named M.properties
  • The file A-validation.xml still exists, but its contents redirect the validation rules to the file M-validation.xml.

These are the only changes we will make to the previous version.

The Netbeans project is as follows:

  • in [1], the [FormInt] action and its validation file
  • in [2], its template [FormIntModel], the validation file for this template, and the messages associated with this template.

Everything else is identical to the previous version.

The [FormInt-validation.xml] file is as follows:


<!--
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//
EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
-->
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//
EN" "http://localhost:8084/example-09B/example/xwork-validator-1.0.2.dtd">
 
<validators>
  <field name="model" >
    <field-validator type="visitor">
      <param name="appendPrefix">false</param>
      <message/>
    </field-validator>
  </field>
</validators>

We won’t attempt to explain the contents of this file. We’ll take it as is, keeping in mind its purpose: to delegate validation rules to the model. You might wonder what this is for. In fact, the model, its validation rules, and its messages may already exist and be used by different actions. Rather than repeating the same validation rules and messages for different actions, it is better to delegate these functionalities to the model.

The file [FormIntModel-validation.xml] is identical to the file [FormInt-validation.xml] from the previous version.

The [FormIntModel.properties] file is identical to the [FormInt.properties] file from the previous version.

Readers are invited to test this new version.