Of course my code *doesn't* work, but I'll leave the reason why as an
exercise for the reader ;}

mvg,
Jasper

On 5/26/06, The Jasper <[EMAIL PROTECTED]> wrote:
Hi,

I just happened to be working on this problem myself. I wrote a validator:

public class dateValidation implements Serializable {

    private static final Log log = LogFactory.getLog(dateValidation.class);


        /* checks two date fields to see if the second field isn't earlier than
         * the first field.
         * Use in combination with Date validator to make sure date format is 
correct.
         */
    public static boolean beginEndDate(Object bean,
            ValidatorAction va, Field field,
            ActionMessages errors,
            Validator validator,
            HttpServletRequest request){

        Date beginDate = null;
        Date endDate   = null;

        String dateBegin   = ValidatorUtils.getValueAsString(bean,
field.getProperty());
        String sProperty2  = field.getVarValue("endDate");
        String dateEnd     = ValidatorUtils.getValueAsString(bean, sProperty2);
        String datePattern = field.getVarValue("datePattern");

        SimpleDateFormat sdf = new SimpleDateFormat(datePattern);

        try{
                beginDate = sdf.parse(dateBegin);
                endDate   = sdf.parse(dateEnd);
        }
        catch (ParseException e){

                if (log.isDebugEnabled()) {
                log.debug("Date parse failed dateBegin =[" + dateBegin
+ "] dateEnd =["+ dateEnd + "], " +
                                          "pattern=[" + datePattern +
"], " + e);
            }
        }

        if (beginDate.compareTo(endDate) <= 0) return true;

        return false;
    }

}

you'll also need to add the following to validation-rules:

     <validator name="beginEndDate"
           
classname="nl.sogyo.intranet.timesheets.customValidators.dateValidation"
              method="beginEndDate"
        methodParams="java.lang.Object,
                      org.apache.commons.validator.ValidatorAction,
                      org.apache.commons.validator.Field,
                      org.apache.struts.action.ActionMessages,
                      org.apache.commons.validator.Validator,
                      javax.servlet.http.HttpServletRequest"
              depends="date"
                  msg="errors.beginEndDate"/>


I haven't tested it yet, so I give no guarantees.

mvg,
Jasper


On 5/25/06, Chaudhary, Harsh <[EMAIL PROTECTED]> wrote:
> Yeah. There is this date comparator at:
> 
http://jakarta.apache.org/commons/validator/apidocs/org/apache/commons/validator/routines/DateValidator.html
>
> It has methods like compareDates etc. But you will need to call this method 
from a Java class and a custom validator seems like a good place. Considering it 
gives you access to a bunch of objects for free like the form bean, fields, the 
request object, errors object etc. You could probably do it in a separate class 
cueing off of the data from the form bean, but I think it would be much harder.
>
>
> Harsh.
>
> -----Original Message-----
> From: Carl Smith [mailto:[EMAIL PROTECTED]
> Sent: Thursday, May 25, 2006 2:31 PM
> To: Struts Users Mailing List
> Subject: RE: Validation for second field must greater than the first field
>
>
> Harsh, Thanks for the quick response.
>
>   I was seeking if  Struts has the internal validator do this things. If not, 
then I agree with you in that I need to write my own custom validator easily. But 
I prefer if Struts already has this sort of comparison validator.
>
>
> "Chaudhary, Harsh" <[EMAIL PROTECTED]> wrote:
>   The solution is to write a custom validator if you want to do it server side using validator. 
In your custom validator, create 2 calendars, one each for start date and end date. Then use the 
method call "before" or "after" on these calendar.
>
> Harsh. Man I got a lot of free time today.
>
> -----Original Message-----
> From: Carl Smith [mailto:[EMAIL PROTECTED]
> Sent: Thursday, May 25, 2006 2:20 PM
> To: Struts Users Mailing List
> Subject: Validation for second field must greater than the first field
>
>
> I am not sure if this is workable, but my requirement was that
>
> Two Fields, startDate and endDate, I need to validate the endDate is 
greater/older than the startDate. Is this something workable/configurable using 
struts validator?
>
> Thanks.
>
>
> ---------------------------------
> Ring'em or ping'em. Make PC-to-phone calls as low as 1ยข/min with Yahoo! 
Messenger with Voice.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
>
> ---------------------------------
> Do you Yahoo!?
>  Get on board. You're invited to try the new Yahoo! Mail Beta.
>
> ---------------------------------------------------------------------
> 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]

Reply via email to