Recipe8.12.Localizing Validation Rules


Recipe 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 attributeslanguage, country, and variantfor 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 language and country, then it searches just for the language, etc. Validations specified for a specific language and country override the same validation configured for the language.

Ensure that the formset for the default locale contains an element for every field you may validate. If the Validator doesn't have a fallback rule for a field, it won't be able to apply the locale-specific rule.


Suppose you have an application that allows the user to enter a currency amount. For English-speaking users, you want to validate that the value contains one or more digits to the left of the decimal, a decimal point, and then two digits to the right of the decimal. You can state using a regular expression:

^\d+\.\d{2}$

The period, or full-stop, character (.) is a special character in a regular expression that matches any character. To match an actual decimal point, you need to escape the period by preceding it with a backslash (\.).


French users, however, use a comma (,) as the decimal separator instead of a period (.). Here's the regular expression for this rule:

^\d+,\d{2}$

In the Solution, the employeeId field is required regardless of locale. For the hourlyRate field, however, you want to allow French-speaking users to enter the value using their natural format for currency.

For each field that you configure in a locale-specific format, you must specify all the rules that apply in the depends attribute. At runtime, the Validator merges the fields between formsets, but it doesn't merge individual rules a field depends on. In the Solution, for example, hourlyRate depends on the required and mask rules. Therefore, all of these dependencies are listed within the French formset.

See Also

The Validator User's Guide at http://struts.apache.org/userGuide/dev_validator.html has a section on localizing validations.

A good thread from the struts-user mailing list discussed how the Validator and the order that it processes fields and form sets. The thread can be traced from http://marc.theaimsgroup.com/?l=struts-user&m=104793541428623&w=2.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net