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

...

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

...

information.

...

Register

...

validators

...

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

...

a

...

file

...

name

...

validators.xml

...

in

...

the

...

root

...

of

...

the

...

classpath

...

(/WEB-INF/classes)

...

with

...

a

...

structure

...

like

...

this:

Code Block
xml
xml
 
{code:xml}
<validators>
    <validator name="required" class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/>
    <validator name="requiredstring" class="com.opensymphony.webwork.validators.JavaScriptRequiredStringValidator"/>
    <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.JavaScriptVisitorFieldValidator"/>
    <validator name="conversion" class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>
</validators>
{code}

h2. Turn 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:
{code:xml}

Turn 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:

Code Block
xml
xml
<interceptor name="validator" class="com.opensymphony.xwork.validator.ValidationInterceptor"/>
{code}

Note:

...

The

...

default

...

*validationWorkflowStack"

...

already

...

includes

...

this.

...

Validation

...

rules

...

can

...

be

...

specified:

...

  1. Per

...

  1. Action

...

  1. class:

...

  1. ActionName-validation.xml

...

  1. Per

...

  1. Action

...

  1. alias:

...

  1. ActionName-alias-validation.xml

...

  1. Inheritance

...

  1. hierarchy

...

  1. and

...

  1. interfaces

...

  1. implemented

...

  1. by

...

  1. Action

...

  1. class:

...

  1. WebWork

...

  1. searches

...

  1. up

...

  1. the

...

  1. inheritance

...

  1. tree

...

  1. of

...

  1. the

...

  1. action

...

  1. to

...

  1. find

...

  1. default

...

  1. validations

...

  1. for

...

  1. parent

...

  1. classes

...

  1. of

...

  1. the

...

  1. Action

...

  1. and

...

  1. interfaces

...

  1. implemented

...

Specify

...

what

...

and

...

how

...

to

...

validate

...

Here

...

is

...

an

...

example

...

for

...

SimpleAction-validation.xml:

Code Block
xml
xml


{code:xml}
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
    <field name="bar">
        <field-validator type="required">
            <message>You must enter a value for bar.</message>
        </field-validator>
        <field-validator type="int">
            <param name="min">6</param>
            <param name="max">10</param>
            <message>bar must be between ${min} and ${max}, current value is ${bar}.</message>
        </field-validator>
    </field>
    <field name="date">
        <field-validator type="date">
            <param name="min">12/22/2002</param>
            <param name="max">12/25/2002</param>
            <message>The date must be between 12-22-2002 and 12-25-2002.</message>
        </field-validator>
    </field>
    <field name="foo">
        <field-validator type="int">
            <param name="min">0</param>
            <param name="max">100</param>
            <message key="foo.range">Could not find foo.range!</message>
        </field-validator>
    </field>
    <validator type="expression">
        <param name="expression">foo > bar</param>
        <message>Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}.</message>
    </validator>
</validators>
{code}

Here

...

we

...

can

...

see

...

the

...

configuration

...

of

...

validators

...

for

...

the

...

SimpleAction

...

class.

...

Validators

...

(and

...

field-validators)

...

must

...

have

...

a

...

type

...

attribute,

...

which

...

refers

...

to

...

a

...

name

...

of

...

an

...

Validator

...

registered

...

with

...

the

...

ValidatorFactory

...

as

...

above.

...

Validator

...

elements

...

may

...

also

...

have

...

<param>

...

elements

...

with

...

name

...

and

...

value

...

attributes

...

to

...

set

...

arbitrary

...

parameters

...

into

...

the

...

Validator

...

instance.

...

See

...

below

...

for

...

discussion

...

of

...

the

...

message

...

element.

...

Each

...

Validator

...

or

...

Field-Validator

...

element

...

must

...

define

...

one

...

message

...

element

...

inside

...

the

...

validator

...

element

...

body.

...

The

...

message

...

element

...

has

...

1

...

attributes,

...

key

...

which

...

is

...

not

...

required.

...

The

...

body

...

of

...

the

...

message

...

tag

...

is

...

taken

...

as

...

the

...

default

...

message

...

which

...

should

...

be

...

added

...

to

...

the

...

Action

...

if

...

the

...

validator

...

fails.

...

Key

...

gives

...

a

...

message

...

key

...

to

...

look

...

up

...

in

...

the

...

Action's

...

ResourceBundles

...

using

...

getText()

...

from

...

LocaleAware

...

if

...

the

...

Action

...

implements

...

that

...

interface

...

(as

...

ActionSupport

...

does).

...

This

...

provides

...

for

...

Localized

...

messages

...

based

...

on

...

the

...

Locale

...

of

...

the

...

user

...

making

...

the

...

request

...

(or

...

whatever

...

Locale

...

you've

...

set

...

into

...

the

...

LocaleAware

...

Action).

...

After

...

either

...

retrieving

...

the

...

message

...

from

...

the

...

ResourceBundle

...

using

...

the

...

Key

...

value,

...

or

...

using

...

the

...

Default

...

message,

...

the

...

current

...

Validator

...

is

...

pushed

...

onto

...

the

...

ValueStack,

...

then

...

the

...

message

...

is

...

parsed

...

for

...

\${...}

...

sections

...

which

...

are

...

replaced

...

with

...

the

...

evaluated

...

value

...

of

...

the

...

string

...

between

...

the

...

\${

...

and

...

}.

...

This

...

allows

...

you

...

to

...

parameterize

...

your

...

messages

...

with

...

values

...

from

...

the

...

Validator,

...

the

...

Action,

...

or

...

both.

...

Here

...

is

...

an

...

example

...

of

...

a

...

parameterized

...

message:

{
Code Block
}
bar must be between ${min} and ${max}, current value is ${bar}.
{code}

This

...

will

...

pull

...

the

...

min

...

and

...

max

...

parameters

...

from

...

the

...

IntRangeFieldValidator

...

and

...

the

...

value

...

of

...

bar

...

from

...

the

...

Action.

...