Kevin Dangoor wrote:
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.
Yes, that's correct. Though now as I look at it, I think I got them
backwards; it would be nice if to_python used the "natural" order, and
to_python used the reverse order. Though in the context of SQLObject
the other way seems more natural -- but that's deep in the guts of
SQLObject, so it's okay with me if it's not as natural feeling.
I'm not sure what exactly to do about this; I doubt anyone *quite* yet
relies on the directionality of All(), except SQLObject. But new
versions of FormEncode and SQLObject would have to be released at the
same time to do the upgrade properly (and not released before then).
Hmm... maybe this will turn into an 0.7.1 release of SQLObject fairly
soon then.
The other option is to add keyword arguments to All() to make it
explicit, so you'd do:
All(to_python=[Int(), OneOf([1, 2, 3])])
Which would make it explicit how you expected the transformations to be
ordered in what context. Transformations will always be reversed for
the other context.
--
Ian Bicking / [EMAIL PROTECTED] / http://blog.ianbicking.org