The following works as expected here (FormEncode==1.2.2):
from formencode import Schema
from formencode.validators import Int
class MySchema(Schema):
def __init__(self, *args, **kw):
kw['myfield'] = Int
Schema.__init__(self, *args, **kw)
print MySchema().to_python({'myfield': '42'}) # gives {'myfield':
42}
then you may want to check your query returns the expected result.
Also be sure your schema is instantiated at the right time, I would
have it inside a controller action (Pylons) or view (Pyramid).
On 21 Lug, 13:30, neurino <[email protected]> wrote:
> Let's take, for example, a User `Schema` where the site admin sets the
> number of requested phone numbers:
>
> class MySchema(Schema):
> name = validators.String(not_empty=True)
> phone_1 = validators.PhoneNumber(not_empty=True)
> phone_2 = validators.PhoneNumber(not_empty=True)
> phone_3 = validators.PhoneNumber(not_empty=True)
> ...
>
> Somehow I thought I could simply do:
>
> class MySchema(Schema):
> name = validators.String(not_empty=True)
> def __init__(self, *args, **kwargs):
> requested_phone_numbers = Session.query(...).scalar()
> for n in xrange(requested_phone_numbers):
> key = 'phone_{0}'.format(n)
> kwargs[key] = validators.PhoneNumber(not_empty=True)
> Schema.__init__(self, *args, **kwargs)
>
> since I read in [FormEncode docs][1]:
>
> > Validators use instance variables to store their customization
> > information. You can use either subclassing or normal instantiation to
> > set these.
>
> and `Schema` is called in docs as a Compound Validator and is a
> subclass of `FancyValidator` so I guessed it's correct.
>
> But this does not work: simply added `phone_n` are ignored and only
> `name` is required.
>
> Also I tried both overriding `__new__` and `__classinit__` before
> asking with no success...
>
> Thanks for your support
>
> P.S.: I asked this on [StackOverflow][2] too, sorry if it can be
> considered crossposting but this task -- I consider it not so uncommon
> -- is driving me mad.
>
> [1]:http://formencode.org/Validator.html#other-validator-usage
> [2]:http://stackoverflow.com/questions/6773957/formencode-schema-add-fiel...
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.