Re: Getting the message key?

Ian Bicking <[email protected]> Wed, 26 Mar 2008 10:14:34 -0500
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
Felix Schwarz wrote:
> Hi,
> 
> in some situations after a validation error occurred, I like to give 
> additional information to the user (hints/explanations why some values are 
> incorrect)
> 
> For example, if a user enters a user name during his registration that is 
> already in use, I want to display some additional information on how to 
> recover lost passwords. This additional information should be "rich content", 
> containing links, images etc.
> 
> But not every error in the user name field should trigger that additional 
> information (e.g. user name is too short/uses banned words). So what I really 
> want is "what type of error occurred".
> 
> This information (type of error) is already encoded in the error message (or 
> it would be trivial to change the validators that way). But clearly, the 
> additional information should not go into the basic validation error message 
> as this should not contain any markup code.
> 
> Currently I see the following options:
> 1. Don't show additional information: ruled out, requirement from customer.
> 2. Repeat all tests in my view to see what happened: Duplication of efforts,
>     error-prone, too much logic in the view.
> 3. Use the error message to decide what information to show: Bad because the
>     exact error message is locale dependent and may change in the future.
> 3a. Compare the error message with all error messages in
>      ValidatorClass.messages to get the key: view has to know which validator
>      was used for for every field which is error prone and fragile.
> 
> Ideally, we would be able to extract the message key from the exception 
> message and use that to show the additional information. After reading the 
> code, I think this is not possible currently.
> 
> Any other ideas?

How about:

   user_validator = ValidatorClass(messages={'notLoggedIn': 
htmlfill.literal('Wrong password.  <a href="...">Did you forget your 
password?</a>')})

Then htmlfill won't quote the error value when putting it in, and you 
can include any markup.

You could also subclass ValidatorClass.message to do dynamic lookup.

   Ian

-------------------------------------------------------------------------
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