Re: Getting the message key?

Andreas Klostermann <andreasklostermann-gM/[email protected]> Wed, 26 Mar 2008 16:08:45 +0100
Newsgroups gmane.comp.python.formencode
Message-ID <1206544125.8653.63.camel@andi-laptop>
Hi again,

In the Tutorial, there is a clear way to do this: Create different
validators with different Messages for each invalid case and chain them
together.

If you are using turbogears you will want to modify the genshi template
to differentiate the cases. The Invalid Exception does contain a lot of
information beyond the error message. For example there is a variable
called "state" which is used to pass on any kind of information you
want. That might be bad API design, but at least it works. See the docs
for more information.

Greetings,
Andreas

Am Mittwoch, den 26.03.2008, 16:01 +0100 schrieb Felix Schwarz:
> Andreas Klostermann wrote:
> > Otherwise you might want to subclass the Invalid exception for
> > communication between Validators and other code. You should however only
> > raise an Invalid exception when the data is "invalid" which means
> > unprocessable. I would not recommend to nag the user a lot about "not
> > perfect" data.
> 
> Its not about "not perfect" data: A user name that is already registered can't 
> be registered again. But I want to show some additional help for the user and 
> in the current situation it is very hard to tell the difference between "user 
> name not allowed" (illegal syntax) and "user name already in use".
> 
> However it would be possible quite easily if I could access the message key.
> 
> One solution I thought about is to patch the messages function in formencode 
> so that it returns not a string but this object:
> 
> class WrappedMsg(object):
>      def __init__(self, msg, key=None):
>          self.msg = msg
>          self.key = key
> 
>      def __repr__(self):
>          return "WrappedMsg<%s: %s>" % (self.key, self.msg)
> 
>      def __str__(self):
>          return self.msg
> 
> If you use that message verbatim in a template engine like genshi, you won't 
> see a difference to a normal message string. Maybe this could be a flag in 
> formencode to active that new behavior...
> 
> If I did not get your point, please elaborate :-)
> 
> fs
> 


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace