📄 dev_validator.html
字号:
<p>
<strong>Multi Page Forms</strong>
</p>
<p>
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.
</p>
<pre>
<code>
<html:hidden property="page" value="1"/>
</code>
</pre>
<p>
<strong>Comparing Two Fields</strong>
</p>
<p>
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.
</p>
<pre>
<code>
<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>
</code>
</pre>
<pre>
<code>
public static boolean validateTwoFields(
Object bean,
ValidatorAction va,
Field field,
ActionErrors errors,
HttpServletRequest request,
ServletContext application) {
String value = ValidatorUtils.getValueAsString(
bean,
field.getProperty());
String sProperty2 = field.getVarValue("secondProperty");
String value2 = ValidatorUtils.getValueAsString(
bean,
sProperty2);
if (!GenericValidator.isBlankOrNull(value)) {
try {
if (!value.equals(value2)) {
errors.add(field.getKey(),
Resources.getActionError(
application,
request,
va,
field));
return false;
}
} catch (Exception e) {
errors.add(field.getKey(),
Resources.getActionError(
application,
request,
va,
field));
return false;
}
}
return true;
}
</code>
</pre>
</div>
<h2 id="validator-bugs">Known Bugs</h2>
<div class="indent">
<p>
Since the Struts Validator relies on the Commons Validator, problem
reports and enhancement requests may be listed against either product.
</p>
<ul>
<li>
<a href="http://nagoya.apache.org/bugzilla/buglist.cgi?bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&bug_status=VERIFIED&bug_severity=Blocker&bug_severity=Critical&bug_severity=Major&bug_severity=Normal&bug_severity=Minor&email1=&emailtype1=substring&emailassigned_to1=1&email2=&emailtype2=substring&emailreporter2=1&bugidtype=include&bug_id=&changedin=&votes=&chfieldfrom=&chfieldto=Now&chfieldvalue=&product=Struts&version=1.1+Beta+1&version=1.1+Beta+2&version=Nightly+Build&version=Unknown&component=Validator+Framework&short_desc=&short_desc_type=allwordssubstr&long_desc=&long_desc_type=allwordssubstr&bug_file_loc=&bug_file_loc_type=allwordssubstr&keywords=&keywords_type=anywords&field0-0-0=noop&type0-0-0=noop&value0-0-0=&cmdtype=doit&order=Bug+Number">
Struts Validator Bugzilla Reports
</a>
</li>
<li>
<a href="http://nagoya.apache.org/bugzilla/buglist.cgi?bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&bug_status=VERIFIED&bug_severity=Blocker&bug_severity=Critical&bug_severity=Major&bug_severity=Normal&bug_severity=Minor&email1=&emailtype1=substring&emailassigned_to1=1&email2=&emailtype2=substring&emailreporter2=1&bugidtype=include&bug_id=&changedin=&votes=&chfieldfrom=&chfieldto=Now&chfieldvalue=&product=Commons&component=Validator&short_desc=&short_desc_type=allwordssubstr&long_desc=&long_desc_type=allwordssubstr&bug_file_loc=&bug_file_loc_type=allwordssubstr&keywords=&keywords_type=anywords&field0-0-0=noop&type0-0-0=noop&value0-0-0=&cmdtype=doit&order=Bug+Number">
Commons Validator Bugzilla Reports</a>
</li>
</ul>
</div>
<h2 id="conditionals">Conditionally required fields</h2>
<div class="indent">
<p>
You can define logic like "only validate this field if field X is
non-null and field Y equals 'male'". The recommended way to do this will
be with the <code>validwhen</code> validator, described above, and available
since Struts 1.2.0. The
<code>requiredif</code> validator, which was added since Struts 1.1,
will be deprecated in favor of <code>validwhen</code>, and
<code>requiredif</code> will be removed in a future release. However, if you
are using <code>requiredif</code>, here is a brief tutorial.
</p>
<p>Let's assume you have a medical information form with three fields, sex, pregnancyTest, and testResult.
If sex is 'f' or 'F', pregnancyTest is required. If pregnancyTest is not blank, testResult is required.
The entry in your Validator configuration would look like this:
</p>
<pre>
<form name="medicalStatusForm">
<field
property="pregnancyTest" depends="requiredif">
<arg0 key="medicalStatusForm.pregnancyTest.label"/>
<var>
<var-name>field[0]</var-name>
<var-value>sex</var-value>
</var>
<var>
<var-name>fieldTest[0]</var-name>
<var-value>EQUAL</var-value>
</var>
<var>
<var-name>fieldValue[0]</var-name>
<var-value>F</var-value>
</var>
<var>
<var-name>field[1]</var-name>
<var-value>sex</var-value>
</var>
<var>
<var-name>fieldTest[1]</var-name>
<var-value>EQUAL</var-value>
</var>
<var>
<var-name>fieldValue[1]</var-name>
<var-value>f</var-value>
</var>
<var>
<var-name>fieldJoin</var-name>
<var-value>OR</var-value>
</var>
</field>
<field
property="testResult" depends="requiredif">
<arg0 key="medicalStatusForm.testResult.label"/>
<var>
<var-name>field[0]</var-name>
<var-value>pregnancyTest</var-value>
</var>
<var>
<var-name>fieldTest[0]</var-name>
<var-value>NOTNULL</var-value>
</var>
</field>
</form>
</pre>
<p>
Here's a more complex example using indexed properties.
</p>
<p>
If you have this in your Struts configuration
</p>
<pre>
<form-bean
name="dependentlistForm"
type="org.apache.struts.webapp.validator.forms.ValidatorForm">
<form-property
name="dependents"
type="org.apache.struts.webapp.validator.Dependent[]" size="10"/>
<form-property
name="insureDependents"
type="java.lang.Boolean"
initial="false"/>
</form-bean>
</pre>
<p>
Where dependent is a bean that has properties lastName, firstName, dob,
coverageType
</p>
<p>
You can define a validation:
</p>
<pre>
<form name="dependentlistForm">
<field
property="firstName" indexedListProperty="dependents"
depends="requiredif">
<arg0 key="dependentlistForm.firstName.label"/>
<var>
<var-name>field[0]</var-name>
<var-value>lastName</var-value>
</var>
<var>
<var-name>fieldIndexed[0]</var-name>
<var-value>true</var-value>
</var>
<var>
<var-name>fieldTest[0]</var-name>
<var-value>NOTNULL</var-value>
</var>
</field>
<field
property="dob"
indexedListProperty="dependents"
depends="requiredif,date">
<arg0 key="dependentlistForm.dob.label"/>
<var>
<var-name>field[0]</var-name>
<var-value>lastName</var-value>
</var>
<var>
<var-name>fieldIndexed[0]</var-name>
<var-value>true</var-value>
</var>
<var>
<var-name>fieldTest[0]</var-name>
<var-value>NOTNULL</var-value>
</var>
</field>
<field
property="coverageType"
indexedListProperty="dependents"
depends="requiredif">
<arg0 key="dependentlistForm.coverageType.label"/>
<var>
<var-name>field[0]</var-name>
<var-value>lastName</var-value>
</var>
<var>
<var-name>fieldIndexed[0]</var-name>
<var-value>true</var-value>
</var>
<var>
<var-name>fieldTest[0]</var-name>
<var-value>NOTNULL</var-value>
</var>
<var>
<var-name>field[1]</var-name>
<var-value>insureDependents</var-value>
</var>
<var>
<var-name>fieldTest[1]</var-name>
<var-value>EQUAL</var-value>
</var>
<var>
<var-name>fieldValue[1]</var-name>
<var-value>true</var-value>
</var>
<var>
<var-name>fieldJoin</var-name>
<var-value>AND</var-value>
</var>
</field>
</form>
</pre>
<p>
Which is read as follows:
The firstName field is only required if the lastName field is non-null.
Since fieldIndexed is true, it means that lastName must be a property of
the same indexed field as firstName.
Same thing for dob, except that we validate for date if not blank.
</p>
<p>
The coverageType is only required if the lastName for the same indexed
bean is not null, and also if the non-indexed field insureDependents is
true.
</p>
<p>
You can have an arbitrary number of fields by using the [n] syntax,
the only restriction is that they must all be AND or OR, you can't mix.
</p>
</div>
<h2 id="stopOnFirstError">Unstoppable JavaScript Validations</h2>
<div class="indent">
<p>
[Since Struts 1.2.0] You can force the clientside Javascript validation
to check all constraints, instead of stopping at the first error.
By setting a new property, <code>stopOnFirstError</code>, on the
Validator PlugIn to false.
</p>
<p>
Here's a sample configuration block that you could use in your
Struts configuration file:
</p>
<pre>
<code>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validations.xml"/>
<set-property property="stopOnFirstError" value="false"/>
</plug-in>
</code>
</pre>
</div>
<h2 id="api">Validator API Guide</h2>
<div class="indent">
<p>
A concise
<a href="../api/org/apache/struts/validator/package-summary.html#package_description">
Struts Validator API Guide</a> is available to help you get started.
</p>
</div>
<h2 id="resources">Validator Resources</h2>
<div class="indent">
<p>
<a href="http://otn.oracle.com/oramag/oracle/04-jan/o14dev_struts.html">
<strong>Check Your Form with Validator</strong>
</a> by James Holmes.
Howto article in Oracle Magazine.
</p>
<p>
<a href="http://www.raibledesigns.com/page/rd/20030226#struts_validator_validating_two_fields">
<strong>Struts Validator: Validating Two Fields Match</strong>
</a> by Matt Raible.
Howto article.
</p>
<p>
<a href="http://www.strutskickstart.com/">
<strong>DynaForms and the Validator</strong>
</a> by James Turner and Kevin Bedell.
Sample chapter from
<a href="http://www.strutskickstart.com/">Struts Kickstart</a>;
available as a free download (PDF).
</p>
<p>
<a href="http://www.manning.com/getpage.html?project=husted&filename=chapters.html">
<strong>Validating user input</strong>
</a> by David Winterfeldt and Ted Husted.
Sample chapter from
<a href="http://www.amazon.com/exec/obidos/ISBN=1930110502/hitchhikeguidetoA/">
Struts in Action</a>;
available as a free download (PDF).
</p>
</div>
</div>
<!--end main-->
</div>
<!--end content-->
<div id="footer">
<img id="powered-logo" alt="Powered by Struts" src="../images/struts-power.gif" />
Copyright (c) 2000-2005, The Apache Software Foundation <span class="noprint">-
<a href="http://wiki.apache.org/struts/StrutsDocComments">Comments?</a>
</span>
</div>
<!--end footer-->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -