Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

If you don't know already, WebWork uses XWork as its foundation, which does provide a validation framework to help you apply WebWork relies on XWork's validation framework to enable the application of input validation rules to your Actions before they are executed. This section provide only enough details to help you get started but always refer to Validation Framework for more informationonly provides the bare minimum to get you started and focuses on WebWork's extension of the XWork validators to support client-side validation. Please consult XWork's validation framework documentation for complete details.

Reference pages

  1. Simple validators
  2. Visitor validation
  3. Client-Side Validation
  4. Validation Examples

Register validators

Registering Validators

Validation rules are handled by validators, which Validators must be registered with the ValidatorFactory. This may either be done programmatically, using the registerValidator(String name, Class clazz) static method of the ValidatorFactory, or by putting The simplest way to do so is to add a file name validators.xml in the root of the classpath (/WEB-INF/classes) with a structure like thisthat declares all the validators you intend to use. The syntax of the file is as follows:

Code Block
xml
xml
<validators>
    <validator name="required" 
        class="com.opensymphony.webwork.validators.JavaScriptRequiredFieldValidator"/>
    <validator name="requiredstring"
        class="com.opensymphony.webwork.validators.JavaScriptRequiredStringValidator"/>
    <validator name="stringlength"
        class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/>
    <validator name="int"
        class="com.opensymphony.webwork.validators.JavaScriptIntRangeFieldValidator"/>
    <validator name="date"
        class="com.opensymphony.webwork.validators.JavaScriptDateRangeFieldValidator"/>
    <validator name="expression"
        class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/>
    <validator name="fieldexpression"
        class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/>
    <validator name="email"
        class="com.opensymphony.webwork.validators.JavaScriptEmailValidator"/>
    <validator name="url"
        class="com.opensymphony.webwork.validators.JavaScriptURLValidator"/>
    <validator name="visitor"
        class="com.opensymphony.webwork.validators.VisitorFieldValidator"/>
    <validator name="conversion"
        class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>
</validators>

...

This list declares all the validators that comes with WebWork.

Turning on Validation

All that is required to enable validation for an Action is to put the ValidationInterceptor in the interceptor refs of the action (see xwork.xml) like so:

...

Note: The default validationWorkflowStack already includes this.

Defining Validation Rules

Validation rules can be specified:

  1. Per Action class: in a file named ActionName-validation.xml
  2. Per Action alias: in a file named ActionName-alias-validation.xml
  3. Inheritance hierarchy and interfaces implemented by Action class: WebWork searches up the inheritance tree of the action to find default validations for parent classes of the Action and interfaces implemented

...

Here is an example for SimpleAction-validation.xml:

...