struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andy Kriger" <akri...@greaterthanone.com>
Subject RE: how do i get a Field object in Validator?
Date Tue, 22 Oct 2002 19:38:27 GMT
Thanks - I've got code like that to validate that 2 fields are equal (sounds
like we could all donate some code to expand the basic rules available to
Validator).

But what I want is the actual Field object (like the one you pass into a
validate method) for another field. I need this so I can get a message
resource from that field (as opposed to passing them in using a var, which I
am now, but don't like because the code is uglier and it duplicates other
validation.xml settings).

-----Original Message-----
From: Dave Derry [mailto:dderry@acm.org]
Sent: Tuesday, October 22, 2002 15:18
To: Struts Users Mailing List
Subject: Re: how do i get a Field object in Validator?


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" <akriger@greaterthanone.com>


> 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>



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


Mime
View raw message