Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

If we're using the default settings and our action doesn't have an "input" result defined and there are validation (or, incidentally, type conversion) errors, we'll get an error message back telling us there's no "input" result defined for the action.

CONTENTS

Table of Contents
stylenone
minLevel2
outlinetrue

Using Annotations

Annotations can be used as an alternative to XML for validation.

...

  1. conversion validator
  2. date validator
  3. double validator
  4. email validator
  5. expression validator
  6. fieldexpression validator
  7. int validator
  8. regex validator
  9. required validator
  10. requiredstring validator
  11. short validator
  12. stringlength validator
  13. url validator
  14. visitor validator
  15. conditionalvisitor validator

Registering Validators

Wiki Markup
{snippet:id=javadoc|javadoc=true|url=com.opensymphony.xwork2.validator/ValidatorFactory.java}

...

Wiki Markup
{snippet:lang=xml|url=resources.com.opensymphony.xwork2.struts2/xwork-core/src/main/resources/com/opensymphony/xwork2/validator/validators/default.xml}
Note
titleStruts 2.1 and Prior

The validators.xml used to reference a DTD hosted by Opensymphony, the original location of the XWork project. Since the the move to Apache Struts, DTDs were changed. Please ensure in your projects to include the DTD header as described in the examples found here

Note
titleStruts 2.0.7 and Prior

The validators.xml containing custom validators needs to contain a copy of the default validators. No DTD was used in validators.xml. See: http://struts.apache.org/2.x/docs/release-notes-208.html#ReleaseNotes2.0.8-MigrationfrompreviousReleases

...

The default interceptor stack, "defaultStack", already has validation turned on. When creating your own interceptor-stack be sure to include both the validation and workflow interceptors. From struts-default.xml:

Code Block
langxml

<interceptor-stack name="defaultStack">
   ...
   <interceptor-ref name="validation">
      <param name="excludeMethods">input,back,cancel,browse</param>
   </interceptor-ref>
   <interceptor-ref name="workflow">
      <param name="excludeMethods">input,back,cancel,browse</param>
   </interceptor-ref>
</interceptor-stack>

Beginning with version 2.0.4 Struts provides an extension to XWork's com.opensymphony.xwork2.validator.ValidationInterceptor interceptor.

Code Block
langxml

<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>

...

Non-Field Validator Vs Field-Validator
Anchor
validatortypes

Wiki Markup
{snippet:id=validatorVsFieldValidators1|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java}
Wiki Markup
{snippet:id=nonFieldValidatorUsingValidatorSyntax|lang=xml|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java}
Wiki Markup
{snippet:id=fieldValidatorUsingValidatorSyntax|lang=xml|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java}
Wiki Markup
{snippet:id=validatorVsFieldValidators2|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java}
Wiki Markup
{snippet:id=fieldValidatorUsingFieldValidatorSyntax|lang=xml|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java}
Wiki Markup
{snippet:id=validatorVsFieldValidators3|javadoc=true|url=com.opensymphony.xwork2.validator/Validator.java}

...

There are two ways you can define validators in your -validation.xml file:

  1. <validator>
  2. <field-validator>

Keep the following in mind when using either syntax:

Non-Field-Validator: The <validator> element allows you to declare both types of validators (either a plain Validator a field-specific FieldValidator).

Code Block
xml
<validator type="expression> 
    <param name="expression">foo gt bar</param> 
    <message>foo must be great than bar.</message> 
</validator> 
Code Block
xml
<validator type="required"> 
    <param name="fieldName">bar</param> 
    <message>You must enter a value for bar.</message> 
</validator> 

field-validator: The <field-validator> elements are basically the same as the <validator> elements except that they inherit the fieldName attribute from the enclosing <field> element. FieldValidators defined within a <field-validator> element will have their fieldName automatically filled with the value of the parent <field> element's fieldName attribute. The reason for this structure is to conveniently group the validators for a particular field under one element, otherwise the fieldName attribute would have to be repeated, over and over, for each individual <validator>.

Tip

It is always better to defined field-validator inside a <field> tag instead of using a <validator> tag and supplying fieldName as its param as the xml code itself is clearer (grouping of field is clearer)

Note

Note that you should only use FieldValidators (not plain Validators) within a block. A plain Validator inside a <field> will not be allowed and would generate error when parsing the xml, as it is not allowed in the defined dtd (xwork-validator-1.0.2.dtd)

Declaring a FieldValidator using the <field-validator> syntax:

Code Block
xml
<field name="email_address"> 
    <field-validator type="required"> 
        <message>You cannot leave the email address field empty.</message> 
    </field-validator> 
    <field-validator type="email"> 
        <message>The email address you entered is not valid.</message> 
    </field-validator> 
</field> 

The choice is yours. It's perfectly legal to only use elements without the elements and set the fieldName attribute for each of them. The following are effectively equal:

Code Block
xml
<field name="email_address"> 
    <field-validator type="required"> 
        <message>You cannot leave the email address field empty.</message> 
    </field-validator> 
    <field-validator type="email"> 
        <message>The email address you entered is not valid.</message> 
    </field-validator> 
</field> 
 
<validator type="required"> 
    <param name="fieldName">email_address</param> 
    <message>You cannot leave the email address field empty.</message> 
</validator> 
<validator type="email"> 
    <param name="fieldName">email_address</param> 
    <message>The email address you entered is not valid.</message> 
</validator> 

Short-Circuiting Validator

...

Resources

WebWork Validation

Next: Localization