Heres a method that I use to validate that one date entered by the user is
earlier than another.

    public static boolean validateDateBefore( Object bean, ValidatorAction
va,
                                                 Field field, ActionErrors
errors,
                                                 HttpServletRequest
request )
    {
        // verify that the bean contains a valid date in the specified
'field'
        Date targetDate = StrutsValidator.validateDate( bean, va, field,
errors, request );
        if( targetDate == null )
            return false;

        // get the date to compare to
        String later = field.getVarValue( "dateBefore" );
        if( GenericValidator.isBlankOrNull( later ) )
        {
            errors.add( field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field) );
            return false;
        }
        String lDate = ValidatorUtil.getValueAsString( bean, later );
        Date laterDate = StrutsValidator.validateDate( lDate, va, field,
errors, request );
        if( lDate == null )
            return false;

        // now we can compare the 2 dates
        if( ! isDateAfter( laterDate, targetDate ) )
        {
            StringBuffer buffer = new StringBuffer( field.getProperty() );
            buffer.append( " must be earlier than " ).append( lDate );
            errors.add( field.getKey(),
StrutsValidatorUtil.getActionError(request, va, field) );
            return false;
        }

        return true;
    }


And this is the entry from validation.xml

    <field property="auditDate"
        depends="date, dateBefore">
     <msg name="date"
       key="Please enter a valid date, if any, that the audit will be done."
       resource ="false"/>
        <msg name="dateBefore"
             key="Audit date must occur before the final date."
             resource="false"/>
        <var>
         <var-name>dateBefore</var-name>
         <var-value>finalDate</var-value>
        </var>
    </field>


The finalDate referenced by var-value is another attribute of my
ValidatorForm.

Does this help?

Dave Derry


----- Original Message -----
From: "Andy Kriger" <[EMAIL PROTECTED]>


> That much I understand - but is it possible to get a reference to the
Field
> objec that represents that 2nd property? For example, if you wanted to get
> message arguments from the other field.
>
> -----Original Message-----
> From: Jerry Jalenak [mailto:Jerry.Jalenak@;LABONE.com]
>
>
> In your validator-rules.xml file, add the following for each form field
you
> need access to:
>
> <form name="yourForm">
> <field property="firstProperty" depends="yourCustomRoutine">
> <var>
> <var-name>secondProperty-Label</var-name>
> <var-value>secondProperty-Value</var-name>
> </var>
> </field>
> </form>
>
> secondProperty-Label would be what you want to call the variable in your
> custom routine; secondProperty-Value would be the actual form field name
> that contains the entered value.  Then in your custom routine you can
access
> these values by
>
> public static boolean yourCustomRoutine(Object object,
> ValidatorAction va, Field field, ActionErrors errors, HttpServletRequest
> request)
> {
> String fp = ValidatorUtil.getValueAsString(object,
> field.getProperty());
> String sp = ValidatorUtil.getValueAsString(object,
> field.getVarValue("secondProperty-Label"));
>
> ... do something ...
> return true;  // or false if the validation fails
> }
>
> HTH,
>
> Jerry
>
> > -----Original Message-----
> > From: Andy Kriger [mailto:akriger@;greaterthanone.com]
> >
> >
> > I am writing a custom rule and I need to know how I can get a
> > handle to a
> > different Field object? (not the one passed into the method
> > in the first
> > place)
> >
> > thx
> > a
> >
> >



--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@;jakarta.apache.org>

Reply via email to