struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Niall Pemberton" <niall.pember...@gmail.com>
Subject Re: HELP, creating my own validation
Date Wed, 28 Mar 2007 20:53:23 GMT
I can't see anything wrong with what you have here. Can you show us
how you have configured Struts to pick up your custom validation rules
and also say what version of Struts and what version of Commons
Validator you are using

Niall

On 3/28/07, Dwight Galloway <dsgallow@utah.gov> wrote:
> I am having troubles getting my own validator to work, I wonder if it is the classpath
where I am pointing to my Class with? I am trying to create a dateRange validation to determin
if the date falls between two dates or not. This is what I have in my validation.xml
>
> >               <field property="startDate" depends="required,date,dateRange">
> >                       <arg0 key="Start Date" resource="false"/>
> >                       <var><var-name>datePattern</var-name><var-value>MM/dd/yyyy</var-value></var>
> >                       <var><var-name>minDate</var-name><var-value>07/01/2003</var-value></var>
> >                                                                <var><var-name>maxDate</var-name><var-value>06/30/2004</var-value></var>
> >               </field>
>
> > this is what I have in my validator-rules
>
> >       <validator name="dateRange"
> >               classname="gov.utah.dhs.pats.util.ValidationUtil"
> >                method="validateDateRange"
> >          methodParams="java.lang.Object,
> >       org.apache.commons.validator.ValidatorAction,
> >       org.apache.commons.validator.Field,
> >                       org.apache.struts.action.ActionMessages,
> >                       javax.servlet.http.HttpServletRequest"
> >               depends=""
> >                   msg="errors.dateRange"/>
>
> >  and my class looks like this, notice I have a println at the start of my program,
it is like it is completely ignoring my class, I am clueless how to test this and to see what
is really happening, I don't have much experience with struts so please any suggestions would
help,thank you...
>
> > package gov.utah.dhs.pats.util;
> > import java.text.SimpleDateFormat;
> > import java.util.Date;
> > import javax.servlet.http.HttpServletRequest;
> > import org.apache.commons.validator.Field;
> > import org.apache.commons.validator.ValidatorAction;
> > import org.apache.commons.validator.util.ValidatorUtils;
> > import org.apache.struts.action.ActionMessages;
> > import org.apache.struts.validator.Resources;
> > public class ValidationUtil {
> >         public static final String DEFAULT_FORM_DATE_PATTERN = "M/dd/yyyy";
> >         public static final Date DEFAULT_MIN_DATE = getDefaultMinDate();
> >         public static final Date DEFAULT_MAX_DATE = getDefaultMaxDate();
> >         public static boolean validateDateRange(Object bean, ValidatorAction va,
Field field, ActionMessages msg, HttpServletRequest request) {
> >               //if we're not properly configured to parse dates,
> >               //all date validation will fail
> >               System.out.println("!!!!!!!!!!!!!INSIDE THE VALIDATION UTILITY the
FIELD.. " + field );
> >               if (DEFAULT_MIN_DATE == null || DEFAULT_MAX_DATE == null) return false;
> >               try {
> >                 String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
> >                 Date date = getDate(value, DEFAULT_FORM_DATE_PATTERN);
> >                 Date minDate = getDate(field.getVarValue("minDate"),  DEFAULT_FORM_DATE_PATTERN);
> >                 Date maxDate = getDate(field.getVarValue("maxDate"), DEFAULT_FORM_DATE_PATTERN);
> >                 if (date.compareTo(minDate) < 0 || date.compareTo(maxDate) >
0) {
> >                       msg.add(field.getKey().toString(), Resources.getActionMessage(request,
va, field));
> >                       return false;
> >                 }
> >                 return true;
> >               }
> >               catch (Exception e) {
> >                 e.printStackTrace();
> >                 msg.add(field.getKey().toString(), Resources.getActionMessage(request,
va, field));
> >                 return false;
> >               }
> >         }
> >
> >       protected static Date getDate(String dateString, String pattern) {
> >         Date date = null;
> >         try {
> >               SimpleDateFormat df = new SimpleDateFormat(pattern);
> >               date = df.parse(dateString);
> >         }
> >         catch (Exception e) {
> >               e.printStackTrace();
> >         }
> >         return date;
> >       }
> >       protected static Date getDefaultMinDate() {
> >         return getDate("01/01/1900", DEFAULT_FORM_DATE_PATTERN);
> >       }
> >       protected static Date getDefaultMaxDate() {
> >         return getDate("12/31/2030", DEFAULT_FORM_DATE_PATTERN);
> >       }
> >   }
>
> > if anyone can please just help guide me a bit on this I would appreciate it.
>
>
> Dwight S. Galloway
> (801) 538-4234
> dsgallow@utah.gov
> Department of Technology Services
> MAKE IT A GREAT DAY!
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

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


Mime
View raw message