commons-user mailing list archives

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

All,

I've been happily using commons-validator 1.3.1 with Struts 1.3 for some
years, now, and I've recently developed my own custom validator that
checks for XHTML syntactic validity. Technically, it checks for XHTML
validity only when the input looks like this: "<html>....</html>":
everything else validates successfully with no actual XHTML syntax check.

Recently, it became clear that users were sometimes forgetting the
"</html>" at the end of the input. This caused two problems: first, the
input wasn't being validated as XHTML, and second, when the data was
then displayed in a web page, it was escaped instead of being rendered
as actual HTML.

My first instinct was to add a "mask" validator that checked for an
appropriate pattern (that is, "<html>" up front with no trailing
"</html>"). I implemented that and it works quite well. Then, it
occurred to me that I might want some other pattern for some reason for
those fields, and that the correct place for that check ought to be in
the XHTML validator itself.

So I started moving the check described above into my custom validator.
Everything was fine until I had to render the error message. Since I'm
doing essentially two checks (one for <html> and matching </html>, and a
second for actually checking the XHTML syntax), I need two distinct
error messages. The configuration for a validator only allows one single
"msg" attribute as shown in my configuration below:

      <validator name="possibleXhtml"
            classname="....PossibleXhtmlValidator"
               method="validatePossibleXhtml"
         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=""
                  msg="errors.xhtml" />

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?

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

iEYEARECAAYFAkyjgOEACgkQ9CaO5/Lv0PBkWwCgjCeACyxBWyv7C4017lfBeMB0
W6AAnioNsVh8uTgjWCPMIHuKIi6AUTLU
=7DAj
-----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