Thanks Ben, I am looking at the code closer now, but do we have to pass in the array 
of selections to the ActionForm class from the jsp....

Regards,
Samy.

-----Original Message-----
From: Ben Anderson [mailto:[EMAIL PROTECTED]
Sent: Tuesday, February 10, 2004 2:28 PM
To: [EMAIL PROTECTED]
Subject: RE: Struts check box validation question


Here's code I got from Kris Schneider.  It validates that at least one of 
the check boxes was checked, but you should be able to change it fairly 
easily to do whatever you want.  If you're wondering how this code fits in, 
check out:
http://jakarta.apache.org/struts/userGuide/dev_validator.html
and look at validateTwoFields to see how to setup of custom validate 
methods.

    public static boolean validateRequiredArray(Object bean,
                                                ValidatorAction va,
                                                Field field,
                                                ActionErrors errors,
                                                HttpServletRequest request) 
{
        boolean isValid = false;

        LOG.debug("bean: " + bean);

        Object[] array = null;
        if (isArray(bean)) {
            array = (Object[])bean;
        } else {
            Object fieldProperty = null;
            try {
                fieldProperty = PropertyUtils.getProperty(bean, 
field.getProperty());
            } catch (Exception exc) {
                // TODO: throw runtime exception?
                LOG.error(exc.getMessage(), exc);
            }
            LOG.debug("fieldProperty: " + fieldProperty);
            if (isArray(fieldProperty)) {
                array = (Object[])fieldProperty;
            }
        }

        if (array != null) {
            for (int i = 0, n = array.length; i < n; i++) {
                Object obj = array[i];
                LOG.debug("array[" + i + "]: '" + obj + "'");
                String value = ((obj == null) ? null : obj.toString());
                if (!GenericValidator.isBlankOrNull(value)) {
                    isValid = true;
                    break;
                }
            }
        }

        if (!isValid) {
            errors.add(field.getKey(), Resources.getActionError(request, va, 
field));
        }

        LOG.debug("isValid: " + isValid);

        return isValid;
    }

    public static boolean isArray(Object obj) {
        return ((obj == null) || obj.getClass().isArray());
    }
}

-Ben

>From: "Samyukta Akunuru" <[EMAIL PROTECTED]>
>Reply-To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
>To: <[EMAIL PROTECTED]>
>Subject: Struts check box validation question
>Date: Tue, 10 Feb 2004 12:57:23 -0600
>
>Had a quick question on power of struts form validation.Sample code to 
>validate checkboxes checked on the jsp, which will be sent as an array to 
>the Struts Action Form. Tips appreciate.Thanks in advance!
>
>Best Regards,
>Samy
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>

_________________________________________________________________
Plan your next US getaway to one of the super destinations here. 
http://special.msn.com/local/hotdestinations.armx


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to