⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dev_validator.html

📁 struts api,学习使用struts必备的文档
💻 HTML
📖 第 1 页 / 共 3 页
字号:

    <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>
&lt;html:hidden property="page" value="1"/&gt;
</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>
&lt;validator name="twofields"
       classname="com.mysite.StrutsValidator"
       method="validateTwoFields"
       msg="errors.twofields"/&gt;

&lt;field property="password"
       depends="required,twofields"&gt;
          &lt;arg0 key="typeForm.password.displayname"/&gt;
          &lt;var&gt;
             &lt;var-name&gt;secondProperty&lt;/var-name&gt;
             &lt;var-value&gt;password2&lt;/var-value&gt;
          &lt;/var&gt;
&lt;/field&gt;
</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&amp;bug_status=NEW&amp;bug_status=ASSIGNED&amp;bug_status=REOPENED&amp;bug_status=VERIFIED&amp;bug_severity=Blocker&amp;bug_severity=Critical&amp;bug_severity=Major&amp;bug_severity=Normal&amp;bug_severity=Minor&amp;email1=&amp;emailtype1=substring&amp;emailassigned_to1=1&amp;email2=&amp;emailtype2=substring&amp;emailreporter2=1&amp;bugidtype=include&amp;bug_id=&amp;changedin=&amp;votes=&amp;chfieldfrom=&amp;chfieldto=Now&amp;chfieldvalue=&amp;product=Struts&amp;version=1.1+Beta+1&amp;version=1.1+Beta+2&amp;version=Nightly+Build&amp;version=Unknown&amp;component=Validator+Framework&amp;short_desc=&amp;short_desc_type=allwordssubstr&amp;long_desc=&amp;long_desc_type=allwordssubstr&amp;bug_file_loc=&amp;bug_file_loc_type=allwordssubstr&amp;keywords=&amp;keywords_type=anywords&amp;field0-0-0=noop&amp;type0-0-0=noop&amp;value0-0-0=&amp;cmdtype=doit&amp;order=Bug+Number">
        Struts Validator Bugzilla Reports
        </a>
        </li>

        <li>
        <a href="http://nagoya.apache.org/bugzilla/buglist.cgi?bug_status=UNCONFIRMED&amp;bug_status=NEW&amp;bug_status=ASSIGNED&amp;bug_status=REOPENED&amp;bug_status=VERIFIED&amp;bug_severity=Blocker&amp;bug_severity=Critical&amp;bug_severity=Major&amp;bug_severity=Normal&amp;bug_severity=Minor&amp;email1=&amp;emailtype1=substring&amp;emailassigned_to1=1&amp;email2=&amp;emailtype2=substring&amp;emailreporter2=1&amp;bugidtype=include&amp;bug_id=&amp;changedin=&amp;votes=&amp;chfieldfrom=&amp;chfieldto=Now&amp;chfieldvalue=&amp;product=Commons&amp;component=Validator&amp;short_desc=&amp;short_desc_type=allwordssubstr&amp;long_desc=&amp;long_desc_type=allwordssubstr&amp;bug_file_loc=&amp;bug_file_loc_type=allwordssubstr&amp;keywords=&amp;keywords_type=anywords&amp;field0-0-0=noop&amp;type0-0-0=noop&amp;value0-0-0=&amp;cmdtype=doit&amp;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>

&lt;form name="medicalStatusForm"&gt;

&lt;field
    property="pregnancyTest" depends="requiredif"&gt;
  &lt;arg0 key="medicalStatusForm.pregnancyTest.label"/&gt;
  &lt;var&gt;
    &lt;var-name&gt;field[0]&lt;/var-name&gt;
    &lt;var-value&gt;sex&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldTest[0]&lt;/var-name&gt;
    &lt;var-value&gt;EQUAL&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldValue[0]&lt;/var-name&gt;
    &lt;var-value&gt;F&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;field[1]&lt;/var-name&gt;
    &lt;var-value&gt;sex&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldTest[1]&lt;/var-name&gt;
    &lt;var-value&gt;EQUAL&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldValue[1]&lt;/var-name&gt;
    &lt;var-value&gt;f&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldJoin&lt;/var-name&gt;
    &lt;var-value&gt;OR&lt;/var-value&gt;
  &lt;/var&gt;
&lt;/field&gt;

&lt;field
    property="testResult" depends="requiredif"&gt;
  &lt;arg0 key="medicalStatusForm.testResult.label"/&gt;
  &lt;var&gt;
    &lt;var-name&gt;field[0]&lt;/var-name&gt;
    &lt;var-value&gt;pregnancyTest&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldTest[0]&lt;/var-name&gt;
    &lt;var-value&gt;NOTNULL&lt;/var-value&gt;
  &lt;/var&gt;
&lt;/field&gt;
&lt;/form&gt;
</pre>

    <p>
    Here's a more complex example using indexed properties.
    </p>

    <p>
    If you have this in your Struts configuration
    </p>

<pre>
&lt;form-bean
    name="dependentlistForm"
    type="org.apache.struts.webapp.validator.forms.ValidatorForm"&gt;
    &lt;form-property
        name="dependents"
        type="org.apache.struts.webapp.validator.Dependent[]" size="10"/&gt;
    &lt;form-property
        name="insureDependents"
        type="java.lang.Boolean"
        initial="false"/&gt;
&lt;/form-bean&gt;
</pre>

    <p>
    Where dependent is a bean that has properties lastName, firstName, dob,
    coverageType
    </p>

    <p>
    You can define a validation:
    </p>

<pre>

&lt;form name="dependentlistForm"&gt;

&lt;field
    property="firstName" indexedListProperty="dependents"
    depends="requiredif"&gt;
  &lt;arg0 key="dependentlistForm.firstName.label"/&gt;
  &lt;var&gt;
    &lt;var-name&gt;field[0]&lt;/var-name&gt;
    &lt;var-value&gt;lastName&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldIndexed[0]&lt;/var-name&gt;
    &lt;var-value&gt;true&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldTest[0]&lt;/var-name&gt;
    &lt;var-value&gt;NOTNULL&lt;/var-value&gt;
  &lt;/var&gt;
&lt;/field&gt;

&lt;field
    property="dob"
    indexedListProperty="dependents"
    depends="requiredif,date"&gt;
  &lt;arg0 key="dependentlistForm.dob.label"/&gt;
  &lt;var&gt;
    &lt;var-name&gt;field[0]&lt;/var-name&gt;
    &lt;var-value&gt;lastName&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldIndexed[0]&lt;/var-name&gt;
    &lt;var-value&gt;true&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldTest[0]&lt;/var-name&gt;
    &lt;var-value&gt;NOTNULL&lt;/var-value&gt;
  &lt;/var&gt;
&lt;/field&gt;

&lt;field
    property="coverageType"
    indexedListProperty="dependents"
    depends="requiredif"&gt;
  &lt;arg0 key="dependentlistForm.coverageType.label"/&gt;
  &lt;var&gt;
    &lt;var-name&gt;field[0]&lt;/var-name&gt;
    &lt;var-value&gt;lastName&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldIndexed[0]&lt;/var-name&gt;
    &lt;var-value&gt;true&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldTest[0]&lt;/var-name&gt;
    &lt;var-value&gt;NOTNULL&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;field[1]&lt;/var-name&gt;
    &lt;var-value&gt;insureDependents&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldTest[1]&lt;/var-name&gt;
    &lt;var-value&gt;EQUAL&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldValue[1]&lt;/var-name&gt;
    &lt;var-value&gt;true&lt;/var-value&gt;
  &lt;/var&gt;
  &lt;var&gt;
    &lt;var-name&gt;fieldJoin&lt;/var-name&gt;
    &lt;var-value&gt;AND&lt;/var-value&gt;
  &lt;/var&gt;
&lt;/field&gt;

&lt;/form&gt;

</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>

  &lt;plug-in className="org.apache.struts.validator.ValidatorPlugIn"&gt;
  &lt;set-property property="pathnames"
   value="/WEB-INF/validator-rules.xml,/WEB-INF/validations.xml"/&gt;
  &lt;set-property property="stopOnFirstError" value="false"/&gt;
  &lt;/plug-in&gt;
  

</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&amp;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 + -