|
Recipe 8.12. Localizing Validation RulesProblemYou need to specify different validation rules for a specific language or country for certain fields on a form. SolutionFirst, 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> DiscussionYou can tell the Validator to use specific field validation rules for a particular locale. The formset supports the standard locale properties with these attributes:
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.
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}$
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 AlsoThe 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. |
|