Recipe8.1.Reusing Validator Attribute Values


Recipe 8.1. Reusing Validator Attribute Values

Problem

You want to define, in one place, a common value you can reference wherever needed in a Validator form.

Solution

Define the value as a global or form-set constant in one of your validation documents, as shown in Example 8-3.

Example 8-3. Defining global and form-set validator constants
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE form-validation PUBLIC  "-//Apache Software Foundation//DTD Commons Validator Rules   Configuration 1.1//EN"  "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd"> <form-validation>     <global>         <constant>             <constant-name>globalVarName</constant-name>             <constant-value>globalVarValue</constant-value>         </constant>     </global>     <formset>         <constant>             <constant-name>formsetVarName</constant-name>             <constant-value>formsetVarValue</constant-value>         </constant>                <form name="MyForm">             <field property="myfield"                     depends="someRule,anotherRule">                 <var>                     <var-name>someRule</var-name>                     <var-value>${globalVarName}</var-value>                 </var>                 <var>                     <var-name>anotherRule</var-name>                     <var-value>${formsetVarName}</var-value>                 </var>             </field>         </form>     <formset> <form-validation>

Discussion

Good developers understand the Don't Repeat Yourself (DRY) principle of software engineering. But if they fail to follow this principle, they could end up all wet. The Validator embraces this guideline, making it an excellent choice for input validation. If you have an attribute value shared by multiple field elements, you can define the value as a named constant. You can reference the constant value by name wherever an attribute value can be used.

If the value applies across the entire application, define it as a global constant. If the value only applies to a specific form and it's used multiple times on that form, define it as a formset constant. In Example 8-4, the minimum length for the username is defined as a global constant. For the RegistrationForm, the maximum length for the first and last name is defined as a formset constant.

Example 8-4. Using global and formset constants
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE form-validation PUBLIC  "-//Apache Software Foundation//DTD Commons Validator Rules   Configuration 1.1//EN"  "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd"> <form-validation>     <global>         <constant>             <constant-name>usernameMinLength</constant-name>             <constant-value>7</constant-value>         </constant>     </global>     <formset>         <constant>             <constant-name>nameMaxLength</constant-name>             <constant-value>40</constant-value>         </constant>                <form name="RegistrationForm">             <field property="username"                     depends="required,minlength">                 <arg key="prompt.username"/>                 <arg key="${var:minlength}" name="minlength"                    resource="false"/>                 <var>                     <var-name>minlength</var-name>                     <var-value>${usernameMinLength}</var-value>                 </var>             </field>             <field property="firstName"                     depends="required,maxlength">                 <arg key="prompt.firstName"/>                 <arg key="${var:maxlength}" name="maxlength"                    resource="false"/>                 <var>                     <var-name>maxlength</var-name>                     <var-value>${nameMaxLength}</var-value>                 </var>             </field>             <field property="lastName"                     depends="required,minlength,maxlength">                 <arg key="prompt.lastName"/>                 <arg key="${var:minlength}" name="minlength"                    resource="false"/>                 <arg key="${var:maxlength}" name="maxlength"                    resource="false"/>                 <var>                     <var-name>minlength</var-name>                     <var-value>2</var-value>                 </var>                 <var>                     <var-name>maxlength</var-name>                     <var-value>${nameMaxLength}</var-value>                 </var>             </field>         </form>     <formset> <formset-validation>

Validator constants are handy for those fields you use throughout your application. If you use the mask validator to check fields such as Social Security Numbers (SSNs) and phone numbers, using a Validator constant means you only have to maintain the regular expression in one place.

See Also

The mask validator is discussed in Recipe 8.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