Skip to Main Content
Jakarta Struts Cookbook
book

Jakarta Struts Cookbook

by Bill Siggelkow
February 2005
Intermediate to advanced content levelIntermediate to advanced
528 pages
12h 53m
English
O'Reilly Media, Inc.
Content preview from Jakarta Struts Cookbook

8.12. Localizing Validation Rules

Problem

You need to specify different validation rules for a specific language or country for certain fields on a form.

Solution

First, define the form validation rules for all form fields within the global formset element in the validation.xml file:

<formset>
    <form name="LocalizedForm">
        <field property="employeeId"
                depends="required">
            <arg key="prompt.employeeId"/>
        </field>
        <field property="hourlyRate"
                depends="required,mask">
            <arg key="prompt.hourlyRate"/>
            <var>
                <var-name>mask</var-name>
                <var-value>^\d+\.\d{2}$</var-value>
            </var>
       </field>
    </form>
</formset>

Then create a new formset element using localization attributes—language, country, and variant—for the specific locale. Within this formset, you can configure locale-specific validation rules for those fields that require special treatment:

<formset language="fr">
    <form name="LocalizedForm">
        <field property="hourlyRate"
                depends="required,mask">
            <arg key="prompt.hourlyRate"/>
            <var>
                <var-name>mask</var-name>
                <var-value>^\d+,\d{2}$</var-value>
            </var>
        </field>
    </form>        
</formset>

Discussion

You can tell the Validator to use specific field validation rules for a particular locale. The formset supports the standard locale properties with these attributes:

  • language

  • country

  • variant

At runtime, the Validator searches the formsets, based on locale, for a matching form definition. It evaluates the locale using the standard search algorithm used for resource bundles; that is, if it can't find its match for the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Jakarta Struts

Programming Jakarta Struts

Chuck Cavaness
Beginning Spring Framework 2

Beginning Spring Framework 2

Bruce Snyder, Sing Li, Anne Horton, Thomas Van de Velde, Naveen Balani, Christian Dupuis
Java Cookbook

Java Cookbook

Ian F. Darwin
Struts 2 in Action

Struts 2 in Action

J. Scott Stanlick, Chad Michael Davis, Donald J. Brown

Publisher Resources

ISBN: 059600771XSupplemental ContentErrata Page