You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by James Turner <tu...@blackbear.com> on 2002/10/23 04:55:20 UTC

Re: struts-user Digest 23 Oct 2002 02:31:16 -0000 Issue 2166

At 10:31 PM 10/22/2002,  "Andy Kriger" <ak...@greaterthanone.com> wrote:
>From: "Andy Kriger" <ak...@greaterthanone.com>
>To: "Struts Users Mailing List" <st...@jakarta.apache.org>
>Subject: RE: how do i get a Field object in Validator?
>Date: Tue, 22 Oct 2002 16:13:23 -0400
>Message-ID: <OJ...@greaterthanone.com>
>MIME-Version: 1.0
>Content-Type: text/plain;
>         charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>
>Essentially, yes. Lets say I have 2 fields A & B. I am validating A but it
>requires B. If B fails, I'd like to get the arg0 value from B's Field object
>and use that in the error message (since there's a key to the display name
>in that arg).
>
>I've worked around this by passing in keys as a separate var but like I said
>earlier, it's ugly.

You might want to start by looking at the new "requiredif" rule in the 
latest Struts builds, it takes advantage of changes in Commons Validator 
that lets you access the Validator (and from it, all of it's resources) in 
any rule.

For example, to get the value of field X in a rule:

public static boolean validateBasedOnX(Object bean,
                                                           ValidatorAction 
va, Field field,
                                                           ActionErrors errors,
                                                           Validator validator,
                                                           HttpServletRequest 
request) {

    Object form = validator.getResource(Validator.BEAN_KEY);
     String dependVal = ValidatorUtil.getValueAsString(form, "X");
.
.
.
}

To get the internal Validator representation of the ageOfTheUniverse field, 
you actually should ask Struts, not the Validator.  This is because the 
Validator doesn't expose it's resources, it expects the call application to 
keep track of them.  In Struts, you can get a handle on the Validator 
resources with:

ValidatorResources res = 
servlet.getServletContext().getAttribute(ValidatorPlugin.VALIDATOR_KEY +
                                                                                               config.getPrefix(), 
resources);

where config is the current ApplicationConfig.

Once you have a handle on the ValidatorResources, you can get a Field 
definition using:

Form form = res.get(locale, formKey);

(where locale is the desired local and formKey is the name of the form.

Field f = form.getFieldMap().get("ageOfTheUniverse");

Hope this helps,
James



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>