FileDocCategorySizeDatePackage
ValidatorRules.javaAPI DocExample1793Wed Aug 11 13:24:02 BST 2004com.oreilly.strutsckbk.ch08

ValidatorRules

public class ValidatorRules extends Object

Fields Summary
Constructors Summary
Methods Summary
public static booleanvalidateMinChoices(java.lang.Object bean, org.apache.commons.validator.ValidatorAction va, org.apache.commons.validator.Field field, org.apache.struts.action.ActionMessages errors, javax.servlet.http.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;