You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Power, David" <Da...@fmr.com> on 2008/03/25 21:59:31 UTC

Commons Validator String array Validation

Hi

I am using commons validator 1.3.1 and am using indexedProperty's for
String[] arrays.
I build up the list on the jsp dynamically so there could be one or more
entries with the same name.

Here is my struts config entry as part of a DynaValidatorForm

<form-property name="abc" type="java.lang.String[]"/>

Then in my custom-forms.xml I have the following configuration rules

<field property="abc" indexedListProperty="abc"
depends="required,integer,intRange">
	<arg key="aRI" resource="false" position="0"/>
	<arg name="intRange" key="${var:min}" resource="false"
position="1"/>
	<arg name="intRange" key="${var:max}" resource="false"
position="2"/>
	<var>
		<var-name>min</var-name>
		<var-value>0</var-value>
	</var>
	<var>
		<var-name>max</var-name>
		<var-value>250</var-value>
	</var>
</field>

This configuration works fine if there are 2 or more fields in the form
with the name 'abc', however when there is only one commons validator
sees it as a string and the following method throws a Validator
Exception

    /**
     * Returns the size of an indexed property from the object we're
validating.
     *
     * @param bean The bean to extract the indexed values from.
     * @throws ValidatorException If there's an error looking up the
property 
     * or, the property found is not indexed.
     */
    private int getIndexedPropertySize(Object bean) throws
ValidatorException {
        Object indexedProperty = null;

        try {
            indexedProperty =
                PropertyUtils.getProperty(bean,
this.getIndexedListProperty());

        } catch(IllegalAccessException e) {
            throw new ValidatorException(e.getMessage());
        } catch(InvocationTargetException e) {
            throw new ValidatorException(e.getMessage());
        } catch(NoSuchMethodException e) {
            throw new ValidatorException(e.getMessage());
        }

        if (indexedProperty == null) {
            return 0;
        } else if (indexedProperty instanceof Collection) {
            return ((Collection)indexedProperty).size();
        } else if (indexedProperty.getClass().isArray()) {
            return ((Object[])indexedProperty).length;
        } else {
            throw new ValidatorException(this.getKey() + " is not
indexed");
        }
    }

The method signature explains this as it says this will be thrown if it
is not an indexed property.

Is what I am trying to achieve possible with the current version of
commons validator? If so is there some configuration changes needed?

Thank you in advance
David 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org