commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: [validator] Possible to configure multiple error messages for a single validator?
Date Wed, 06 Oct 2010 14:42:53 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Niall,

On 10/5/2010 3:05 PM, Niall Pemberton wrote:
>> On Wed, Sep 29, 2010 at 7:09 PM, Christopher Schultz
>> I could simply hard-code the error message key into the validator, but
>> that seems less maintainable than I'd like it to be. Is it possible to
>> provide more than one error message key to a validator like this?
>> 
>> Other options for me include creating a custom validator that does
>> nothing but check for the <html>...</html> that won't interfere with,
>> say, the "mask" validator.
>> 
>> It still seems to me that my goal ought to be a single validator that is
>> basically self-contained, rather than having two validators that
>> essentially /must/ be used together.
>> 
>> Does anyone have any suggestions?
> 
> Its been a long time since I worked with Stuts & Validator, but take a
> look at the *standard* validators defined in struts:
> 
> http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/java/org/apache/struts/validator/FieldChecks.java?view=markup

Yeah, I wrote some date validation code in the past where I was using
user preferences to configure a SimpleDateFormat for parsing. At the
time, I had to read all the code from the Struts validators to figure
out how to bridge the gap between commons-validator and Struts. I've
used my old code as a basis for this new code.

> Its the validation implementation that actually adds the messages to
> the Struts ActionMessages object - usually by calling
> 
>    Resources.getActionMessage(validator, request, va, field))
> 
> http://svn.apache.org/viewvc/struts/struts1/trunk/core/src/main/java/org/apache/struts/validator/Resources.java?view=markup

Here's my code for the "intro without outro" error:

> if(value.regionMatches(true, 0, "<html>", 0, 6))
> {
>   if(!value.regionMatches(true, value.length() - 7, "</html>", 0, 7))
>   {
>     errors.add(field.getKey(),
>       getActionMessage(validator,
>           request,
>           "error.xhtml.header-without-footer",
>           "Clinician",
>           null));
> 
>     return false;
>   }
> }
> else
>   return true;

The "getActionMessage" method unpacks the ServletContext from the
validator, and uses Resources and RequestUtils static methods to grab
the user's Locale and the appropriate MessageResources, then returns an
ActionMessage combining everything together.

You'll note that the code above hard-codes the error message key
("error.xhtml-header-without-footer"). This is precisely what I'm trying
to avoid.

The code that generates the error message for the actual syntax errors
looks like this:

> Collection parseErrors = xhtmlValidator.getErrors();
> if(null != parseErrors && 0 < parseErrors.size())
> {
>   for(Iterator i=parseErrors.iterator(); i.hasNext(); )
>   {
>     SAXParseException spe = (SAXParseException)i.next();
>     errors.add(field.getKey(),
>         getActionMessage(validator,
>         request,
>         va,
>         field,
>         new Object[] {
>           new Integer(spe.getLineNumber() - 1),
>           new Integer(spe.getColumnNumber()),
>           spe.getMessage()
>         }));
>   }
> }

Here, the getActionMessage does much the same work as the method used
above, except that this time the message is not hard-coded. Instead, it
is fetched from either field itself or from the generic validator:

> private static ActionMessage getActionMessage(Validator validator,
>                                               HttpServletRequest request,
>                                               ValidatorAction va,
>                                               Field field,
>                                               Object[] args)
> {
>   // This code was adapted from Struts 1.3.8
>   // org/apache/struts/validator/Resources.getActionMessage()
>   Msg msg = field.getMessage(va.getName());
> 
>   if((null != msg) && !msg.isResource())
>     return new ActionMessage(msg.getKey(), false);
> 
>   String msgKey = null;
>   String msgBundle = null;
> 
>   if(null == msg)
>   {
>     msgKey = va.getMsg();
>   }
>   else
>   {
>     msgKey = msg.getKey();
>     msgBundle = msg.getBundle();
>   }
> 
>   if((null == msgKey) || (0 == msgKey.length()))
>     return new ActionMessage("??? " + va.getName() + "."
>                              + field.getProperty() + " ???", false);
> 
>   return getActionMessage(validator, request, msgKey, msgBundle, args);
> }

That last method call (for the return) is the same as the one made above
with the explicit message key. So, this method basically gets the
pre-configured error key from either the validator-forms.xml <field>
element or the validator-rules.xml <validator> as a fallback (and I just
let it fall-back, since it's the same message every time).

So, I've got all the code I need, really. The problem is just figuring
out a way to emit two different error messages from the same validator.

> So you need to create your own custom getActionMessage() impl - you
> could use the "msg" returned from the field or validator and use that
> as a prefix and append a different suffix for each error.
> 
> Alternatively you could define two separate validators - one for each condition.

I'm leaning toward using a comma-separated "msg" in the validator setup:
before the comma is the "header/footer" error and after the comma is the
syntax error message.

Unless anyone else has a flash of inspiration.

Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkysiuwACgkQ9CaO5/Lv0PDWgACgrGksTEcu6xy8zn5lpUzVntkz
NCUAn2YJAD6NAuif5J49j987XNKCPs0p
=EKmB
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message