Author your own pluggable validator. See the Struts doc on this. I recently posted an example that you can probably use as a place to start (pasted below).

Erik



deepak wrote:

Hi,
   Using XML date validation, how do I check if the entered date is in the 
future or not ?

Thanks

--Deepak



-------- Original Message --------
Subject:        Re: Hmm... i will go crazy due to validation
Date:   Wed, 05 Jan 2005 05:40:26 -0500
From:   Erik Weber <[EMAIL PROTECTED]>
To:     Struts Users Mailing List <user@struts.apache.org>
References:     <[EMAIL PROTECTED]>



This is not a direct answer to any of your questions but perhaps it will spur your thinking.

This is an example of using a custom ("pluggable") validator that makes sure a date entry is not only a real date, but that the date is within a desired range (seemingly you are wanting to do something similar to this). I wrote this for Struts 1.1. I don't know if there is something new and improved that would be better, but it works.

From validation.xml (you'll see the use of the "date" rule -- which
uses the "datePattern" variable, as well as my custom "dateRange" rule -- which uses the "minDate" and "maxDate" variables):

<field property="licenseExpirationDate" depends="date,dateRange">
            <arg0 key="vendor.edit.label.licenseExpirationDate"/>
              <var>
                <var-name>datePattern</var-name>
                  <var-value>MM/dd/yyyy</var-value>
              </var>
              <var>
                <var-name>minDate</var-name>
                  <var-value>01/01/1900</var-value>
              </var>
              <var>
                <var-name>maxDate</var-name>
                  <var-value>12/31/2030</var-value>
              </var>
          </field>



From validator-rules.xml:

<validator name="dateRange"
classname="ValidationUtil"
method="validateDateRange"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
depends="required"
msg="errors.dateRange"/>



And finally the source of ValidationUtil:


import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.text.SimpleDateFormat;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.validator.Resources;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorUtil;
import org.apache.commons.validator.GenericValidator;

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, ActionErrors errors, HttpServletRequest request) {
//if we're not properly configured to parse dates,
//all date validation will fail
if (DEFAULT_MIN_DATE == null || DEFAULT_MAX_DATE == null) return false;
try {
String value = ValidatorUtil.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) {
errors.add(field.getKey(), Resources.getActionError(request, va, field));
return false;
}
return true;
}
catch (Exception e) {
e.printStackTrace();
errors.add(field.getKey(), Resources.getActionError(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);
}

}


Maybe you should browse the Struts wiki (wiki.apache.org/struts) for example links?


Hope that helps some,

Erik

P.S. I never had much luck with DatePatternStrict, always wondered if it was buggy . . . But I guess others are using it successfully?



Manisha Sathe wrote:

I am testing struts validatior in one test program. Finally i could do some 
date validation for user i/p. I could get javascript pop-up if i enter it 
wrongly.

i wanted to test server side validation - so i switched off javascript and 
tried to submit - it went through w/o throwing any error.

Now again pls help me, what i am missing out ? I am using something like 
(instead of ActionForm)

public class MyForm extends  ValidatorForm  {

....

}

inside my formbean

regards
Manisha

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to