You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Bailey, Shane C." <SH...@saic.com> on 2003/08/04 21:09:52 UTC
Validator required not working with String[] multi-select
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.
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org
Re: Validator required not working with String[] multi-select
Posted by Kris Schneider <kr...@dotech.com>.
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." <SH...@saic.com>:
>
> 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.
--
Kris Schneider <ma...@dotech.com>
D.O.Tech <http://www.dotech.com/>
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org