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:
languagecountryvariant
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.
Read now
Unlock full access