On 10/24/05, DaveS <[EMAIL PROTECTED]> wrote:
>
> I'm trying to use a compound validator but seem to be a little
> confused.  Here's a simple example:
>
> >>> x = All(Int(), OneOf([1,2,3]))
> >>> x.to_python('1')
> . remainder of traceback removed .
> formencode.api.Invalid: Value must be one of: 1; 2; 3 (not '1')
>
> This is a little confusing.  After digging through the code it looks
> like Compound validators run the conversion *backwards*, so OneOf gets
> '1' before Int has a chance to convert it to an integer.
> I'm sure there is a logical reason for this.  Could someone enlighten
> me.

I have a vague recollection of this coming up in an SQLObject context.
SQLObject uses the validators for doing conversions to/from database
formats. I believe that the order between to_python and from_python
needed to be reversed in order for the conversions to work logically.
This makes sense, because after you've done the conversion, you have a
different value than you started out with, and you'd want the reverse
operation to be able to work. I guess the issue becomes which
perspective is the "forward" conversion perspective, and which one is
the "backward" perspective.

> BTW.  is there a reason turbogears.validators doesn't include:
>   from formencode.compound import *

No. As a general rule, I don't try to hide things behind a
"turbogears" namespace wrapper, but in this case it seems pleasant to
have all of the validators (including TurboGears-specific ones)
available.

Kevin

Reply via email to