Recipe8.9.Validating Two or More Choices


Recipe 8.9. Validating Two or More Choices

Problem

You need to check that two or more choices have been picked from a set of checkboxes or a list of options.

Solution

Use my minchoices pluggable Validator rule. The rule implementation is shown in Example 8-13.

Example 8-13. Validating minimum choices
package com.oreilly.strutsckbk.ch08; import java.util.Collection; import javax.servlet.http.HttpServletRequest; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.validator.Field; import org.apache.commons.validator.ValidatorAction; import org.apache.struts.action.ActionMessages; import org.apache.struts.validator.Resources; public class ValidatorRules {          public static boolean validateMinChoices(Object bean,                                               ValidatorAction va,                                               Field field,                                               ActionMessages errors,                                              HttpServletRequest request) {         try {             Object values = PropertyUtils.getProperty(bean, field.                                                       getProperty( ));             int minChoices = Integer.parseInt(field.getVarValue                                              ("minChoices"));             if (!(values == null)) {                 int numChoices = 0;                 if (values instanceof Object[]) {                     numChoices = ((Object[])values).length;                 }                 else if (values instanceof Collection) {                     numChoices = ((Collection) values).size( );                 }                 else {                     errors.add(field.getKey( ),                             Resources.getActionMessage(request, va, field));                     return false;                                     }                 if (numChoices < minChoices) {                     errors.add(field.getKey( ),                                Resources.getActionMessage(request, va, field));                     return false;                 }               }         }         catch (Exception e) {             errors.add(field.getKey( ),                     Resources.getActionMessage(request, va, field));             return false;         }                  return true;     } }

Then add the corresponding validator element to the validator-rules.xml file:

<validator name="minchoices"       classname="com.oreilly.strutsckbk.ch08.ValidatorRules"          method="validateMinChoices"    methodParams="java.lang.Object,                  org.apache.commons.validator.ValidatorAction,                  org.apache.commons.validator.Field,                  org.apache.struts.action.ActionMessages,                  javax.servlet.http.HttpServletRequest"         depends="required"             msg="errors.minChoices"/>

Finally, you can apply the rule where needed:

<field property=""         depends="minchoices">     <arg position="0" key="prompt.language"/>     <arg position="1" key="${var:minChoices}"          resource="false"/>     <var>         <var-name>minChoices</var-name>         <var-value>3</var-value>     </var> </field>

Discussion

You often need to verify that users have checked at least one choice from a set of checkboxes or selected one or more options from a select list. Both of these validations can be performed using the Validator's predefined required rule. However, validating users who have chosen more than one item requires a custom rule like the one shown in the Solution.

You create the minchoices rule using the same basic steps outlined in Recipe 8.7. The rule requires that the validated field must be an array or a Collection. If it's not, then the validation fails and an error is returned. Otherwise, the value of the minChoices variable is retrieved. If the size of the array or Collection is less than minChoices, the validation fails; otherwise, the validation passes.

This pluggable validator was developed using the Struts 1.2 API. It uses the ActionMessages and ActionMessage classes instead of ActionErrors and ActionError. The signature of the validateMinChoices( ) method and the methodParams attribute of the validator element specify ActionMessages.

If the methodParams specified in the validator element doesn't match the method signature of the rule's Java method, the Validator will be unable to find the method to invoke.


See Also

Recipe 8.7 provides an additional example of creating custom pluggable validators.



    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