Академический Документы
Профессиональный Документы
Культура Документы
Table of Contents
Preface
Introduction
Model Components
View Components
Controller Components
Configuration
Release Notes
Installation
Developer Guides
Bean Tags
HTML Tags
Logic Tags
Nested Tags
Tiles Tags
Utilities
Validator
Quick Links
Welcome
User and Developer Guides *
FAQs and HowTos
Contributors
David Winterfeldt
James Turner
Rob Leland
Niall Pemberton
Struts Validator Guide
Struts Validator
The Struts Validator, in some form, has been available since the days of Struts
0.5. It was orignally packaged as a developer contribution. Later, the core code
was moved to the Jakarta Commons and a Struts specific extension became part of
Struts since 1.1.
For the convenience of the many developers who have been using the Struts
Validator all along, this document first overviews the core functionality and
then covers the changes and new functionality added since Struts 1.1.
Once you have configured the Validator Plug-In, so that it can load your
Validator Resources you just have to extend
org.apache.struts.validator.action.ValidatorForm instead of
org.apache.struts.action.ActionForm. Then when the validate method is called,
the action's name attribute from the struts-config.xml is used to load the
validations for the current form. So the form element's name attribute in the
validator-rules.xml should match action element's name attribute.
Another alternative is to use the action mapping you are currently on by
extending the ValidatorActionForm instead of the ValidatorForm. The
ValidatorActionForm uses the action element's path attribute from the
struts-config.xml which should match the form element's name attribute in the
validator-rules.xml.
Then a separate action can be defined for each page in a multi-page form and the
validation rules can be associated with the action and not a page number as in
the example of a multi-page form in the validator example.
Internationalization
Validation rules for forms can be grouped under a FormSet element in the
validator-rules.xml file. The FormSet has language, country, and variant
attributes that correspond with the java.util.Locale class. If they are not
used, the FormSet will be set to the default locale. A FormSet can also have
constants associated with it. On the same level as a FormSet there can be a
global element which can also have constants and have validator actions that
perform validations.
Note: You must declare a default FormSet without internationalization before
your internationalized FormSets. This allows the Validator to fall back to the
default version if no locale is found.
The default error message for a pluggable validator can be overriden with the
msg element. So instead of using the msg attribute for the mask validator to
generate the error message the msg attribute from the field will be used if the
name of the field's name attribute matches the validator's name attribute.
The arguments for error messages can be set with the arg0-arg3 elements. If the
arg0-arg3 elements' name attribute isn't set, it will become the default arg
value for the different error messages constructed. If the name attribute is
set, you can specify the argument for a specific pluggable validator and then
this will be used for constructing the error message.
<field
property="lastName"
depends="required,mask">
<msg
name="mask"
key="registrationForm.lastname.maskmsg"/>
<arg0 key="registrationForm.lastname.displayname"/>
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z]*$</var-value>
</var>
</field>
By default the arg0-arg3 elements will try to look up the key attribute in the
message resources. If the resource attribute is set to false, it will pass in
the value directly without retrieving the value from the message resources.
Note that since Struts 1.1, you must explicitly define your message resource in
any module that is going to use the Validator, due to a problem accessing the
top-level resource. This only effects applications which are using modules.
<field
property="integer"
depends="required,integer,intRange">
<arg0 key="typeForm.integer.displayname"/>
<arg1
name="intRange"
key="${var:min}"
resource="false"/>
<arg2
name="intRange"
key="${var:max}"
resource="false"/>
<var>
<var-name>min</var-name>
<var-value>10</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>20</var-value>
</var>
</field>
Standard Built In Validations
Validator is shipped with the following set of pre-defined validation rules.
required - mandatory field validation. Has no variables.
<field property="name" depends="required">
<arg0 key="customer.name"/>
</field>
requiredif - field dependant validator
Deprecated, use validwhen.validwhen - validator for checking one field against a
nother.
see later section titled Designing "Complex Validations with validwhen".minlengt
h - validate input data isn't less than a specified minimum length.
Requires a minlength variable.
<field property="name" depends="required,minlength">
<arg0 key="customer.name"/>
<arg1 name="minlength" key="${var:minlength}" resource="false"/>
<var><var-name>minlength</var-name><var-value>3</var-value></var>
</field>
maxlength - validate input data doesn't exceed a specified maximum length.
Requires a maxlength variable.
<field property="name" depends="required,maxlength">
<arg0 key="customer.name"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
<var><var-name>maxlength</var-name><var-value>30</var-value></var>
</field>
mask - validate format according to a regular expression. Requires a mask
variable to specify the regular expression. Since version 1.1, the regular
expression must start with a ^ and end with a $ (see example below).
<field property="name" depends="required,mask">
<msg name="mask" key="registrationForm.lastname.maskmsg"/>
<arg0 key="registration.name"/>
<var><var-name>mask</var-name><var-value>^[a-zA-Z]*$</var-value></var>
</field>
byte - validates that a field can be converted to a Byte.
<field property="age" depends="byte">
<arg0 key="employee.age"/>
</field>
short - validates that a field can be converted to a Short.
<field property="productnumber" depends="short">
<arg0 key="order.prodno"/>
</field>
integer - validates that a field can be converted to an Integer.
<field property="ordernumber" depends="integer">
<arg0 key="order.number"/>
</field>
long - validates that a field can be converted to a Long.
<field property="ordernumber" depends="long">
<arg0 key="order.number"/>
</field>
float - validates that a field can be converted to a Float.
<field property="amount" depends="float">
<arg0 key="sale.amount"/>
</field>
double - validates that a field can be converted to a Double.
<field property="amount" depends="double">
<arg0 key="sale.amount"/>
</field>
date - validates that a field can be converted to a Date. This validation rule
uses java.text.SimpleDateFormat to parse the date and optionally either a
datePattern or datePatternStrict variable can be used. If no pattern is
specified the default short date format is assumed. The difference between
using the datePatternStrict and datePattern variables is that
datePatternStrict checks additionally that the input data is the same length
as the pattern specified (so for example 1/1/2004 would fail with a pattern of
MM/dd/yyyy).
<field property="saledate" depends="required,date">
<arg0 key="myForm.saledate"/>
<var><var-name>datePattern</var-name><var-value>MM/dd/yyyy</var-value></
var>
</field>
If the Javascript Tag is used, the client side Javascript generation looks for a
value in the validator's javascript attribute and generates an object that the
supplied method can use to validate the form. For a more detailed explanation of
how the Javascript Validator Tag works, see the html taglib API reference.
The 'mask' action lets you validate a regular expression mask to the field. It
uses the Regular Expression Package from the Apache Jakarta site. All validation
rules can be stored in the validator-rules.xml file. The main class used is
org.apache.regexp.RE.
Example Validator Configuration from validation.xml.
<validator name="required"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required">
<validator name="mask"
classname="org.apache.struts.validator.FieldChecks"
method="validateMask"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.invalid">
Creating Pluggable Validators
The methodParams attribute takes a comma separated list of class names. The
method attribute needs to have a signature complying with the above list. The
list can be comprised of any combination of the following:
java.lang.Object - Bean validation is being performed on.
org.apache.commons.validator.ValidatorAction - The current ValidatorAction
being performed.
org.apache.commons.validator.Field - Field object being validated.
org.apache.struts.action.ActionErrors - The errors objects to add an
ActionError to if the validation fails.
javax.servlet.http.HttpServletRequest - Current request object.
javax.servlet.ServletContext - The application's ServletContext.
org.apache.commons.validator.Validator - The current
org.apache.commons.validator.Validator instance.
java.util.Locale - The Locale of the current user.
Multi Page Forms
The field element has an optional page attribute. It can be set to an integer.
All validation for any field on a page less than or equal to the current page is
performed server side. All validation for any field on a page equal to the
current page is generated for the client side Javascript. A mutli-part form
expects the page attribute to be set.
<html:hidden property="page" value="1"/>
Comparing Two Fields
This is an example of how you could compare two fields to see if they have the
same value. A good example of this is when you are validating a user changing
their password and there is the main password field and a confirmation field.
<validator name="twofields"
classname="com.mysite.StrutsValidator"
method="validateTwoFields"
msg="errors.twofields"/>
<field property="password"
depends="required,twofields">
<arg0 key="typeForm.password.displayname"/>
<var>
<var-name>secondProperty</var-name>
<var-value>password2</var-value>
</var>
</field>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validation.xml,/WEB-INF/validator-rules.xml"/>
<set-property property="stopOnFirstError" value="false"/>
</plug-in>