Struts 2 > Simple validators |

Struts 2
Simple validators
Edit Page Browse Space Add Page Add News
Added by ctran, last edited by Ted Husted on Sep 03, 2006 (view change)

The following validators are included in the default validators.xml:

Name | JavaScript aware | Description
--- | --- | ---
#required |  | Field value must have a value (non-null)
#requiredstring | x | Field value is non-null and has a length > 0
#regex |  | If not empty, field value must match a regular expression
#int | x | Field value must be an integer and within a range
#date |  | Field value must be a date (the format is based on locale) and within a range
#expression |  | A given OGNL expression is evaluated against the value stack and must return true. This is mostly usefully for cross-field validation. Errors are added as action errors
#fieldexpression |  | A given OGNL expression is evaluated against the value stack and must return true. This is similar to expression but errors are added as field errors
#email | x | Field value must be a valid e-mail address
#url | x | Field value must be a valid url
[visitor] |  | Allows you to forward validation to object properties of your action using the objects own validation files
conversion |  | Add conversion errors from ActionContext to field errors of the action. This does the same thing as WebWorkConversionErrorInterceptor

Note: the above name can be changed if you supply your own validators.xml.

## required

In SimpleAction-validation.xml:

    <validators>
        <field name="bar">
            <field-validator type="required">
                <message>You must enter a value for bar.</message>
            </field-validator>
        </field>
    </validators>

#top

## requiredstring

In LoginAction-validation.xml:

    <validators>
        <field name="userName">
            <field-validator type="requiredstring">
                <message>You must enter an username.</message>
            </field-validator>
        </field>
    </validators>

The error is shown if request parameter userName is missing or an empty string

#top

## regex

    <validators>
        <field name="phone">
            <field-validator type="regex">
                <param name="regex">\([\d][\d][\d]\) [\d][\d][\d]-[\d][\d][\d][\d]</param>
                <message>Phone number must be in the format (XXX) XXX-XXXX</message>
            </field-validator>
        </field>
    </validators>

#top

## int

    <validators>
        <field name="foo">
            <field-validator type="int">
                <param name="min">0</param>
                <param name="max">100</param>
                <message key="foo.range">Could not find foo.range!</message>
            </field-validator>
        </field>
    </validators>

#top

## date

    <validators>
        <field name="startDate">
            <field-validator type="date">
                <param name="min">12/22/2002</param>
                <param name="max">12/25/2002</param>
                <message>The date must be between 12-22-2002 and 12-25-2002.</message>
            </field-validator>
        </field>
    </validators>

#top

## expression

    <validators>
        <validator type="expression">
            <param name="expression">foo > bar</param>
            <message>Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}.</message>
        </validator>
    </validators>

The validator is not associated with a single field. You may need to place your expression within a CDATA if it contains bad xml characters.

#top

## fieldexpression

    <validators>
         <field name="productCode">
            <field-validator type="fieldexpression">
                <param name="expression">name.length() == 5</param>
                <message>Product code must be 5 characters, it is currently '${productCode}'</message>
            </field-validator>
        </field>
    </validators>

#top

## email

    <validators>
        <field name="email">
            <field-validator type="email">
                <message>You must enter a valid email address.</message>
            </field-validator>
        </field>
    </validators>

The address must be in the format xxx@yyy.com|net|gov|org|edu|info|mil|biz|tv|...

#top

## url

    <validators>
         <field name="companyUrl">
            <field-validator type="url">
                <message>You must enter a valid URL.</message>
            </field-validator>
        </field>
    </validators>

#top