Please do file a bug report.

Thanks,

Rob

Bailey, Shane C. wrote:

It sure appears that way from my code analysis.

I JUST made it work though with a simple update of the commons source code.
I guess I could look into submitting my update if they / anyone is
interested in incorporating it in the commons code.


I changed the org.apache.commons.validator.ValidatorUtil.getValueAsString() method from:

 public static String getValueAsString(Object bean, String property) {
     Object value = null;

     try {
        value = PropertyUtils.getProperty(bean, property);      
     } catch (Exception e) {
        log.error(e.getMessage(), e);
     }
     return (value != null ? value.toString() : null);          
  }

to:

 public static String getValueAsString(Object bean, String property) {
     Object value = null;

try {
value = PropertyUtils.getProperty(bean, property);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
//Special case, check if String[]
try
{
String[] valueArray = (String[])value; if(valueArray==null || valueArray.length==0)
{
value = null;
}
}
catch(ClassCastException cce)
{
//Then it wasn't a String[]
}
return (value != null ? value.toString() : null);
}



I guess, just to be safe the second catch could catch Exception.


Anyway, it fixed my validation problem for String[] and doesn't appear to
have broken and validations which worked before.




-----Original Message-----
From: Kris Schneider [mailto:[EMAIL PROTECTED] Sent: Monday, August 04, 2003 3:52 PM
To: Struts Users Mailing List
Subject: Re: Validator required not working with String[] multi-select


AFAIK, the standard "required" validator doesn't work with arrays. Or, if it
does, I wasted time writing a custom validator ;-). I'd be intersted to hear
if
anyone really has used the "required" validator successfully for an array
field.
I suppose I should check to see if there's a bug/RFE for an array validator
and
submit it...

Quoting "Bailey, Shane C." <[EMAIL PROTECTED]>:



I sort of already sent this message to the list but I made a mistake
(didn't
proof read my message) so here it is again but corrected ...

Validator "required" validation is NOT working for my String[] (in a
dynaform.  I have seen others on the list say it works for them.

Anyone else experiencing the same problem? Possibly it is because I am
using
a multi-select?

Basically, if nothing is selected then instead of null be sent back for
that
parameter I am getting a zero length String[]and it's toString() value is
"[Ljava.lang.String;@3cfaab" which is important because of the way the
validateRequired() method works...

I looked at the source code (of my commons-validator.jar using jad).
Maybe I am using an older version of the commons-validator jar and this


was


fixed or I am doing something else wrong?

If so, where is the latest commons-validator.jar?

Here is how my field is declared on the JSP:

<html:select property="roles" multiple="true" size="7">
<html:options collection="<%=my.Const.ROLE_TYPES%>" property="roleName"/>
</html:select>




Here is how it is declared in the struts config:

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




----------------------------------------------------------------------------



Struts source Code:


public static boolean validateRequired(Object bean,
ValidatorAction va, Field


field,


ActionErrors errors,
HttpServletRequest request) {


String value = null;

if (isString(bean)) {

value = (String) bean;

} else {

           value = ValidatorUtil.getValueAsString(bean,
                field.getProperty());

}

if (GenericValidator.isBlankOrNull(value)) {

errors.add(field.getKey(),

Resources.getActionError(request, va, field));

return false;

} else {

return true;

}



}





Problem:

ValidatorUtil.getValueAsString(bean, field.getProperty()) does a


toString()


on the value and a toString() on a String[] which isn't null it is some String like "[Ljava.lang.String;@3cfaab" and
so
then isBlankOrNull() is checking for null OR value.trim().length==0 and so
neither is true and so validation passes.







--
-----
Rob Leland (703-525-3580)


Choose a job you love, and you will never have to work a day of your life. -Confucius.



Reply via email to