In this example, we have an Action that attempts to fill the values of a User object. The first validation file would be for a a simple Action that had a "user" property. The second validation file is for the exact same Action if it had implemented the ModelDriven interface and exposed the User object via getModel() instead of getUser(). Both of them uses the VisitorFieldValidator to use the User object's own validation rules to validate the User object, which can be found in the third validation file.

Notice that the short-circuit attribute of the field-validator element is used in several places to prevent subsequent validators from running. This is useful in preventing multiple error messages when one will do. For example, if the value provided for the User's startDate cannot be parsed as a date, display the appropriate message and don't even bother checking that it is in the correct range.


SampleAction-validation.xml:

<validators>
    <field name="user">
        <field-validator type="required"  short-circuit="true">
            <message>You must provide a user.</message>
        </field-validator>
        <field-validator type="visitor">
            <param name="context">anotherContext</param>
            <message>user: </message>
        </field-validator>
    </field>
</validators>


SampleModelDrivenAction-validation.xml:

<validators>
    <field name="model">
        <field-validator type="visitor">
            <param name="appendPrefix">false</param>
            <message />
        </field-validator>
    </field>
</validators>


User-validation.xml:

<validators>
    <field name="userName">
        <field-validator type="requiredstring" short-circuit="true">
            <message>You must enter an username.</message>
        </field-validator>
        <field-validator type="stringlength">
            <param name="minLength">3</param>
            <param name="maxLength">10</param>
            <message>Username must be between ${minLength} and ${maxLength} characters long.</message>
        </field-validator>
    </field>
    <field name="email">
        <field-validator type="email">
            <message>You must enter a valid email address.</message>
        </field-validator>
    </field>
     <field name="homepage">
        <field-validator type="url" short-circuit="true">
            <message>You must enter a valid URL.</message>            
        </field-validator>
        <field-validator type="fieldexpression">
            <param name="expression">homepage.indexOf('opensymphony.com') == -1</param>
            <message>Please provide an OpenSymphony website</message>
        </field-validator>
    </field>
    <field name="age">
        <field-validator type="int">
            <param name="min">0</param>
            <param name="max">100</param>
            <message>Not a valid age!</message>
        </field-validator>
    </field>
    <field name="startDate">
        <field-validator type="conversion" short-circuit="true">
            <validator type="startDate">
            <message>Could not convert input to a valid date.</message>
        </field-validator>
        <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>