1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<document url="dev_validator.html">
<!--
// ======================================================================== 78
-->
<properties>
<author>David Winterfeldt</author>
<author>James Turner</author>
<author>Rob Leland</author>
<title>The Struts User's Guide - Validator Guide</title>
</properties>
<body>
<section name="Struts Validator Guide"/>
<section href="validator" name="Struts Validator">
<p>
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 1.1.
</p>
<p>
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 in the Struts 1.1.
</p>
<p>
Once you have configured the Validator Plug-In, so that it can load your
Validator Resources you just have to extend
<code>org.apache.struts.validator.action.ValidatorForm</code> instead of
<code>org.apache.struts.action.ActionForm</code>.
Then when the <code>validate</code> 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 <code>name</code> attribute in the
validator-rules.xml should match action element's <code>name</code>
attribute.
</p>
<p>
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 <code>path</code>
attribute from the struts-config.xml which should match the form element's
<code>name</code> attribute in the validator-rules.xml.
</p>
<p>
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.
</p>
</section>
<section href="i18n" name="Internationalization">
<p>
Validation rules for forms can be grouped under a <code>FormSet</code>
element in the validator-rules.xml file.
The <code>FormSet</code> has language, country, and variant attributes that
correspond with the <code>java.util.Locale</code> class.
If they are not used, the <code>FormSet</code> will be set to the default
locale.
A <code>FormSet</code> can also have constants associated with it.
On the same level as a <code>FormSet</code> there can be a global element
which can also have constants and have validator actions that perform
validations.
</p>
<p>
<strong>Note</strong>: You must declare a default <code>FormSet</code> without
internationalization before your internationalized <code>FormSet</code>s. This
allows the Validator to fall back to the default version if no locale is found.
</p>
<p>
The default error message for a pluggable validator can be overriden with
the <code>msg</code> element.
So instead of using the <code>msg</code> attribute for the mask validator
to generate the error message the <code>msg</code> attribute from the
field will be used if the name of the field's name attribute matches the
validator's name attribute.
</p>
<p>
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.
</p>
<pre><code><![CDATA[
<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>
]]></code></pre>
<p>
By default the arg0-arg3 elements will try to look up the <code>key</code>
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.
</p>
<p>
Note that as of the Struts 1.1 release, 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.
</p>
<pre><code><![CDATA[
<field
property="integer"
depends="required,integer,intRange">
<arg0 key="typeForm.integer.displayname"/>
<arg1
name="range"
key="${var:min}"
resource="false"/>
<arg2
name="range"
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>
</section>
<section href="members" name="Constants/Variables">
<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><![CDATA[
<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><![CDATA[
<field
property="integer"
depends="required,integer,intRange">
<arg0 key="typeForm.integer.displayname"/>
<arg1
name="range"
key="${var:min}"
resource="false"/>
<arg2
name="range"
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>
</section>
<section href="validwhen" name="Designing Complex Validations with validwhen">
<p>
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 new <code>validwhen</code>
validation rule, which will be included into the Struts release
immediately after the 1.1 release, is designed to handle these cases.</p>
<p>
The <code>validwhen</code> rule 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 the validwhen rule as:</p>
<pre><code><![CDATA[
<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><![CDATA[
<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><![CDATA[
<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>
</section>
<section href="plugs" name="Pluggable Validators">
<p>
Validation actions are read from the validation.xml file.
The default actions are setup in the validation.xml file.
The ones currently configured are required, mask ,byte, short, int, long,
float, double, date (without locale support), and a numeric range.
</p>
<p>
The 'mask' action depends on required in the default setup.
That means that 'required' has to successfully completed before 'mask' will
run.
The 'required' and 'mask' action are partially built into the framework.
Any field that isn't 'required' will skip other actions if the field is null
or has a length of zero.
</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' action lets you validate a regular expression mask to the field.
It uses the Regular Expression Package from the jakarta site.
All validation rules can be stored in the validator-rules.xml file.
The main class used is <code>org.apache.regexp.RE</code>.
</p>
<p>
Example Validator Configuration from validation.xml.
</p>
<pre><code><![CDATA[
<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>
<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><![CDATA[
<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><![CDATA[
<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><![CDATA[
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>
</section>
<section href="validator-bugs" name="Known Bugs">
<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://issues.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://issues.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>
</section>
<section href="validator-changes" name="Changes and deprecations">
<p>
<strong>New tag attributes.</strong>
</p>
<p>
The <a href="struts-html.html#javascript"><html:javascript> tag</a>
has new attributes defined.
</p>
<p>
<strong>Validating against the DTD in the commons-validator.jar.</strong>
</p>
<p>
The validator xml files now <strong>validates against the DTD stored
in the commons-validator.jar </strong>!
Struts no longer maintains a separate dtd for validator-rules.xml and
validator.xml.
Additionally, commons-validator now maintains a unified validator.dtd.
Change all validator.xml DTD references to:
</p>
<pre><![CDATA[
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">
]]></pre>
<p>
<strong>Blank fields.</strong>
</p>
<p>
The default validator-rules.xml now ignores blank fields for all the
basic validation types.
If you require a field to be present then to your applications
validator.xml field definition add "required" to the depends
attribute.
</p>
<p>
<strong>New range methods.</strong>
</p>
<p>
<code>intRange</code> & <code>floatRange</code> methods in both
JavaScript and Java
</p>
<p>
<strong>Conditionally required fields.</strong>
</p>
<p>
The most fundamental change is the ability to conditionally require
validator fields based on the value of other fields.
It allows you to 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> rule, described above, and available
in the first release after 1.1. The
<code>requiredif</code> validation rule, which was added in Struts 1.1,
will be deprecated in favor of this rule, and 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 validation.xml file 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-config.xml
</p>
<pre><![CDATA[
<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><![CDATA[
<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>
<p>
<strong>Deprecations.</strong>
</p>
<ul>
<li>
Deprecation of <code>range</code> methods in both JavaScript and Java.
</li>
<li>
Deprecation of StrutsValidator & StrutsValidatorUtil.
</li>
</ul>
</section>
<section href="tiles" name="Validator API Guide">
<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>
</section>
<section href="resources" name="Validator Resources">
<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://tinyurl.com/6jnv">
<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>
</section>
</body>
</document>
|