Return-Path: Delivered-To: apmail-commons-user-archive@www.apache.org Received: (qmail 2301 invoked from network); 5 Oct 2010 19:05:32 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 5 Oct 2010 19:05:32 -0000 Received: (qmail 46725 invoked by uid 500); 5 Oct 2010 19:05:31 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 46552 invoked by uid 500); 5 Oct 2010 19:05:30 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Received: (qmail 46544 invoked by uid 99); 5 Oct 2010 19:05:30 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 Oct 2010 19:05:30 +0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of niall.pemberton@gmail.com designates 209.85.214.43 as permitted sender) Received: from [209.85.214.43] (HELO mail-bw0-f43.google.com) (209.85.214.43) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 Oct 2010 19:05:25 +0000 Received: by bwz16 with SMTP id 16so5302508bwz.30 for ; Tue, 05 Oct 2010 12:05:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=Iwo2JjhctMZhWlJbdL8P9iqJuqMsCB0HtCkKkK+JjLI=; b=k58GexYCMXqNckwEdu07K/S2k16hhUtLrnbR2/lszrqhzW2gtcRG+837eBLH9EAhJD 4lJI4LmiM0H2KhLx/vh70sxGu0vRTumembZBTjWOMnus0Bi8BvhSUk99IjmiJI8u/n90 OHTPt5E2BdlmTMzKFZ61045LPQ7JWQ8J4OES4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=PKtkO+x6gtpJingPGJLD1uOtCKOFCzaEbv/z0JWlqkCtMyposOGxiuCEa9jm6VFJz/ pPhYfFwSiI6ij2IRQr53WPyWNi1yuf0kMAygASTDXHVvqJiO0ZVMmYo9X68nNkzYfWU7 eSdz1X0BtbTTkX9sM7RUtMBwWx3UcUF7yiJFI= MIME-Version: 1.0 Received: by 10.204.78.136 with SMTP id l8mr1622982bkk.63.1286305504394; Tue, 05 Oct 2010 12:05:04 -0700 (PDT) Received: by 10.204.127.18 with HTTP; Tue, 5 Oct 2010 12:05:04 -0700 (PDT) In-Reply-To: <4CA380E1.4030706@christopherschultz.net> References: <4CA380E1.4030706@christopherschultz.net> Date: Tue, 5 Oct 2010 20:05:04 +0100 Message-ID: Subject: Re: [validator] Possible to configure multiple error messages for a single validator? From: Niall Pemberton To: Commons Users List Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Wed, Sep 29, 2010 at 7:09 PM, Christopher Schultz wrote: > -----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: "....": > everything else validates successfully with no actual XHTML syntax check. > > Recently, it became clear that users were sometimes forgetting the > "" 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, "" up front with no trailing > ""). 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 and matching , 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: > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0classname=3D"....PossibleXhtmlValidator" > =A0 =A0 =A0 =A0 =A0 =A0 =A0 method=3D"validatePossibleXhtml" > =A0 =A0 =A0 =A0 methodParams=3D"java.lang.Object, > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 org.apache.commons.validator.= ValidatorAction, > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 org.apache.commons.validator.= Field, > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 org.apache.struts.action.Acti= onMessages, > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 org.apache.commons.validator.= Validator, > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 javax.servlet.http.HttpServle= tRequest" > =A0 =A0 =A0 =A0 =A0 =A0 =A0depends=3D"" > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0msg=3D"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 ... 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/ap= ache/struts/validator/FieldChecks.java?view=3Dmarkup 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/ap= ache/struts/validator/Resources.java?view=3Dmarkup 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 condi= tion. Niall > Thanks, > - -chris > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.10 (MingW32) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ > > iEYEARECAAYFAkyjgOEACgkQ9CaO5/Lv0PBkWwCgjCeACyxBWyv7C4017lfBeMB0 > W6AAnioNsVh8uTgjWCPMIHuKIi6AUTLU > =3D7DAj > -----END PGP SIGNATURE----- > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org > For additional commands, e-mail: user-help@commons.apache.org > > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@commons.apache.org For additional commands, e-mail: user-help@commons.apache.org