📄 dev_validator.html
字号:
which must also be in the field's <code>depends</code> attribute.
<pre>
<code>
<field property="age" depends="required,integer,intRange">
<arg0 key="employee.age"/>
<arg1 name="intRange" key="${var:min}" resource="false"/>
<arg2 name="intRange" key="${var:max}" resource="false"/>
<var><var-name>min</var-name><var-value>18</var-value></var>
<var><var-name>max</var-name><var-value>65</var-value></var>
</field>
</code>
</pre>
</li>
<li>
<strong>floatRange</strong> - validates that a float field is within a specified range
Requires <code>min</code> and <code>max</code> variables to specify the range.
This validator depends on the <code>float</code> validator
which must also be in the field's <code>depends</code> attribute.
<pre>
<code>
<field property="ordervalue" depends="required,float,floatRange">
<arg0 key="order.value"/>
<arg1 name="floatRange" key="${var:min}" resource="false"/>
<arg2 name="floatRange" key="${var:max}" resource="false"/>
<var><var-name>min</var-name><var-value>100</var-value></var>
<var><var-name>max</var-name><var-value>4.99</var-value></var>
</field>
</code>
</pre>
</li>
<li>
<strong>doubleRange</strong> - validates that a double field is within a specified range
Requires <code>min</code> and <code>max</code> variables to specify the range.
This validator depends on the <code>double</code> validator
which must also be in the field's <code>depends</code> attribute.
<pre>
<code>
<field property="ordervalue" depends="required,double,doubleRange">
<arg0 key="employee.age"/>
<arg1 name="doubleRange" key="${var:min}" resource="false"/>
<arg2 name="doubleRange" key="${var:max}" resource="false"/>
<var><var-name>min</var-name><var-value>100</var-value></var>
<var><var-name>max</var-name><var-value>4.99</var-value></var>
</field>
</code>
</pre>
</li>
<li>
<strong>creditCard</strong> - validate credit card number format
<pre>
<code>
<field property="name" depends="required, creditCard">
<arg0 key="customer.cardnumber"/>
</field>
</code>
</pre>
</li>
<li>
<strong>email</strong> - validate email address format
<pre>
<code>
<field property="customeremail" depends="email">
<arg0 key="customer.email"/>
</field>
</code>
</pre>
</li>
<li>
<strong>url</strong> - validates url format.
Has four <i>optional</i> variables (<code>allowallschemes</code>,
<code>allow2slashes</code>, <code>nofragments</code> and <code>schemes</code>)
which can be used to configure this validator.
<ul>
<li>
<strong>allowallschemes</strong> specifies whether all schemes are allowed. Valid
values are <code>true</code> or <code>false</code> (default is <code>false</code>).
If this is set to <code>true</code> then the <code>schemes</code> variable is ignored.</li>
<li>
<strong>allow2slashes</strong> specifies whether double '/' characters are allowed. Valid
values are <code>true</code> or <code>false</code> (default is <code>false</code>).</li>
<li>
<strong>nofragments</strong> specifies whether fragements are allowed. Valid
values are <code>true</code> or <code>false</code> (default is <code>false</code> - i.e.
fragments are allowed).</li>
<li>
<strong>schemes</strong> - use to specify a comma separated list of valid schemes. If
not specified then the defaults are used which are <code>http</code>,
<code>https</code> and <code>ftp</code>.</li>
</ul>
<pre>
<code>
<field property="custUrl" depends="url">
<arg0 key="customer.url"/>
</field>
<field property="custUrl" depends="url">
<arg0 key="customer.url"/>
<var>
<var-name>nofragments</var-name>
<var-value>true</var-value>
</var>
<var>
<var-name>schemes</var-name>
<var-value>http,https,telnet,file</var-value>
</var>
</field>
</code>
</pre>
</li>
</ul>
</div>
<h2 id="members">Constants/Variables</h2>
<div class="indent">
<p>
Global constants can be inside the global tags and FormSet/Locale
constants can be created in the formset tags.
Constants are currently only replaced in the Field's property attribute,
the Field's var element value attribute, the Field's msg element key
attribute, and Field's arg0-arg3 element's key attribute.
A Field's variables can also be substituted in the arg0-arg3
elements (ex: ${var:min}).
The order of replacement is FormSet/Locale constants are replaced first,
Global constants second, and for the arg elements variables are replaced
last.
</p>
<pre>
<code>
<global>
<constant>
<constant-name>zip</constant-name>
<constant-value>^\d{5}(-\d{4})?$</constant-value>
</constant>
</global>
<field
property="zip"
depends="required,mask">
<arg0 key="registrationForm.zippostal.displayname"/>
<var>
<var-name>mask</var-name>
<var-value>${zip}</var-value>
</var>
</field>
</code>
</pre>
<p>
The var element under a field can be used to store variables for use by a
pluggable validator.
These variables are available through the Field's
<code>getVar(String key)</code> method.
</p>
<pre>
<code>
<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>
</code>
</pre>
</div>
<h2 id="validwhen">Designing Complex Validations with validwhen</h2>
<div class="indent">
<p>
[Since Struts 1.2.0] A frequent requirement in validation design is to validate one field
against another (for example, if you have asked the user to type in
a password twice for confirmation, to make sure that the values match.)
In addition, there are fields in a form that may only be required if
other fields have certain values. The <code>validwhen</code>
validator is designed to handle these cases.</p>
<p>
The <code>validwhen</code> validator takes a single <code>var</code> field,
called <code>test</code>. The value of this var is a boolean expression
which must be true in order for the validation to success. The
values which are allowed in the expression are:</p>
<ul>
<li>Single or double-quoted string literals.</li>
<li>Integer literals in decimal, hex or octal format</li>
<li>The value <code>null</code> which will match against either
null or an empty string</li>
<li>Other fields in the form referenced by field name, such as
<code>customerAge</code>
</li>
<li>Indexed fields in the form referenced by an explicit integer,
such as <code>childLastName[2]</code>
</li>
<li>Indexed fields in the form referenced by an implicit integer,
such as <code>childLastName[]</code>, which will use the same
index into the array as the index of the field being tested.</li>
<li>Properties of an indexed fields in the form referenced by an
explicit or implicit integer, such as <code>child[].lastName</code>,
which will use the same index into the array as the index of the
field being tested.</li>
<li>The literal <code>*this*</code>, which contains the value of
the field currently being tested</li>
</ul>
<p>
As an example of how this would work, consider a form with
fields <code>sendNewsletter</code> and <code>emailAddress</code>.
The <code>emailAddress</code> field is only required if the
<code>sendNewsletter</code> field is not null. You could code
this using validwhen as:</p>
<pre>
<code>
<field property="emailAddress" depends="validwhen">
<arg0 key="userinfo.emailAddress.label"/>
<var>
<var-name>test</var-name>
<var-value>((sendNewsletter == null) or (*this* != null))</var-value>
</var>
</field>
</code>
</pre>
<p>
Which reads as: this field is valid if <code>sendNewsletter</code> is
<code>null</code> or the field value is not <code>null</code>.</p>
<p>
Here's a slightly more complicated example using indexed fields.
Assume a form with a number of lines to allow the user to enter
part numbers and quantities they wish to order. An array of
beans of class <code>orderLine</code> is used to hold the entries in
a property called orderLines.
If you wished to verify that every line with part number also had
a quantity entered, you could do it with:</p>
<pre>
<code>
<field property="quantity" indexedListProperty="orderLines" depends="validwhen">
<arg0 key="orderform.quantity.label"/>
<var>
<var-name>test</var-name>
<var-value>((orderLines[].partNumber == null) or (*this* != null))</var-value>
</var>
</field>
</code>
</pre>
<p>
Which reads as: This field is value if the corresponding <code>partNumber
</code> field is <code>null</code>, or this field is not <code>null</code>.
</p>
<p>
As a final example, imagine a form where the user must enter their
height in inches, and if they are under 60 inches in height, it is
an error to have checked off nbaPointGuard as a career.</p>
<pre>
<code>
<field property="nbaPointGuard" depends="validwhen">
<arg0 key="careers.nbaPointGuard.label"/>
<var>
<var-name>test</var-name>
<var-value>((heightInInches >= 60) or (*this* == null))</var-value>
</var>
</field>
</code>
</pre>
<p>
A few quick notes on the grammer.</p>
<ul>
<li>All comparisons must be enclosed in parens.</li>
<li>Only two items may be joined with <code>and</code> or <code>or</code>
</li>
<li>If both items to be compared are convertable to ints, a numeric
comparison is done, otherwise a string comparison is done.
</li>
</ul>
</div>
<h2 id="plugs">Pluggable Validators</h2>
<div class="indent">
<p>
By convention, the validators your application uses can beloaded through a
file named "validator-rules.xml", and the validator forms (or "validations")
can be configured separately (say, in a "validations.xml" file). This
approach separates the validators, that you might reuse in another
application, from the validations that are specific to each application.
</p>
<p>
The Validator comes bundled with several ready-to-use validators. The
bundled validators include: required, mask ,byte, short, int, long,
float, double, date (without locale support), and a numeric range.
</p>
<p>
The 'mask' validator depends on 'required' in the default setup.
That means that 'required' has to complete successfully before 'mask' will
run.
The 'required' and 'mask' validators are partially built into the framework.
Any field that isn't 'required' will skip other validations if the field is null
or has a length of zero. Regardless, the implementations of 'required' and 'mask'
are still plugged in through the configuration file, like all the others.
</p>
<p>
If the <a href="struts-html.html#javascript">Javascript Tag</a> 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 <a href="struts-html.html">html taglib API reference</a>.
</p>
<p>
The 'mask' validator lets you validate a regular expression mask to the field.
It uses the Regular Expression Package from the Apache Jakarta site.
</p>
<p>
The main class used is <code>org.apache.regexp.RE</code>.
</p>
<p>
Example Validator Configuration from the default validator-rules.xml.
</p>
<pre>
<code>
<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">
</code>
</pre>
<p>
<strong>Creating Pluggable Validators</strong>
</p>
<p>
The <code>methodParams</code> attribute takes a comma separated list
of class names. The <code>method</code> attribute needs to have a signature
complying with the above list. The list can be comprised of any combination
of the following:
</p>
<ul>
<li>
<code>java.lang.Object</code>
- Bean validation is being performed on.
</li>
<li>
<code>org.apache.commons.validator.ValidatorAction</code>
- The current ValidatorAction being performed.
</li>
<li>
<code>org.apache.commons.validator.Field</code>
- Field object being validated.
</li>
<li>
<code>org.apache.struts.action.ActionErrors</code>
- The errors objects to add an ActionError to if the validation fails.
</li>
<li>
<code>javax.servlet.http.HttpServletRequest</code>
- Current request object.
</li>
<li>
<code>javax.servlet.ServletContext</code>
- The application's ServletContext.
</li>
<li>
<code>org.apache.commons.validator.Validator</code>
- The current org.apache.commons.validator.Validator instance.
</li>
<li>
<code>java.util.Locale</code>
- The Locale of the current user.
</li>
</ul>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -