A few years ago I wrote my own validator which checked whether the
listbox had at least one
thing selected. Below is the validator
(http://javaboutique.internet.com/tutorials/Struts11Val/index-14.html)
helped me get going. Also below is a validator-rules-custom.xml that
needs to be added to the struts config
like so;
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,
/WEB-INF/validator-rules-custom.xml"/>
</plug-in>
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.Resources;
public class MultilistValidator {
private static final Log log =
LogFactory.getLog(MultilistValidator.class);
public static boolean validateRequiredMultilist(Object bean,
ValidatorAction va, Field field, ActionMessages errors,
HttpServletRequest request) {
try {
String[] s = (String[]) PropertyUtils.getProperty(bean,
field.getProperty());
if ((s == null) || (s.length == 0)) {
errors.add(field.getKey(),
Resources.getActionMessage(request,va, field));
return false;
}
} catch (Exception ex) {
log.fatal(ex, ex);
errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}
return true;
}
}
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules
Configuration 1.2.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_2_0.dtd">
<form-validation>
<global>
<!-- added by mas see
http://javaboutique.internet.com/tutorials/Struts11Val/index-14.html -->
<validator name="requiredmultilist"
classname="org.ycmi.validators.MultilistValidator"
method="validateRequiredMultilist"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
msg="errors.multilist">
</validator>
</global>
</form-validation>
Sahil Gupta wrote:
Hi,
Thanks a lot
Can I do put the validation with the help of validation.xml to a String[]
Regards,
Sahil Gupta
-----Original Message-----
From: Strachan, Paul [mailto:[EMAIL PROTECTED]
Sent: Thursday, September 21, 2006 6:11 PM
To: Struts Users Mailing List
Subject: RE: Validation on String[]
<form-property name="locations" type="java.util.ArrayList"/>
<field property="locationName" indexedListProperty="locations"
depends="required">
<arg key="Location" resource="false"/>
</field>
in your action class:
ArrayList list = new ArrayList();
Location location = new Location();
location.setLocationName("A Location");
list.add(location);
dyForm.set("locations", list);
I'm not sure how to validate a simple array of strings (eg
java.lang.String[]) as the property attribute is required by the validator.
________________________________
From: Sahil Gupta [mailto:[EMAIL PROTECTED]
Sent: Thu 21/09/2006 8:40 PM
To: 'Struts Users Mailing List'
Subject: RE: Validation on String[]
Hi,
How can we use List or ArrayList in DynaValidator Forms?
Regards,
Sahil Gupta
-----Original Message-----
From: Li [mailto:[EMAIL PROTECTED]
Sent: Thursday, September 21, 2006 3:57 PM
To: Struts Users Mailing List
Subject: Re: Validation on String[]
hi,
there is no such type : java.lang.String[], try to use List or ArrayList
On 9/21/06, Sahil Gupta <[EMAIL PROTECTED]> wrote:
Hi,
I have used a dyna form in which I have a property which is a
multi-select pick list defined as
form-property name="location" type="java.lang.String[]
Now I want to put validation (required and/or mask) on this. Can
anyone please guide me. I want it to show an error if the user clicks
on submit button without selecting anything in the picklist. I have
handled this validation in my Action but I want to do this through
validation.xml
Regards,
Sahil Gupta
--
When we invent time, we invent death.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
**********************************************************************
This message is intended for the addressee named and may contain privileged
information or confidential information or both. If you are not the intended
recipient please delete it and notify the sender.
**********************************************************************
---------------------------------------------------------------------
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]
--
Mark Shifman MD. Ph.D.
Yale Center for Medical Informatics
Phone (203)737-5219
[EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]