struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Samyukta Akunuru" <Samyukta.Akun...@info-engineering.com>
Subject RE: Struts check box validation question
Date Tue, 10 Feb 2004 20:30:34 GMT
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:stuts_andersonbd1@hotmail.com]
Sent: Tuesday, February 10, 2004 2:28 PM
To: struts-user@jakarta.apache.org
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" <Samyukta.Akunuru@info-engineering.com>
>Reply-To: "Struts Users Mailing List" <struts-user@jakarta.apache.org>
>To: <struts-user@jakarta.apache.org>
>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: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>

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


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Mime
View raw message