Discussion:
[FE-discuss] Combine different Schema classes together
Andrea Riciputi
2009-03-05 16:50:05 UTC
Permalink
Hi,
I have a form composed of several fieldsets. Since I need to re-use
those fieldsets over and over again in many forms, I thought to write
a Schema object for each fieldset, and then combine them in a super-
validator. Something like this:

class Foo(Schema):
allow_extra_fields = True
filter_extra_fields = True
... foo validators here ...
pass

class Bar(Schema):
allow_extra_fields = True
filter_extra_fields = True
... bar validators here ...
pass

class Gnus(Schema):
allow_extra_fields = True
filter_extra_fields = True
... gnus validators here ...
pass

class SuperSchema(Foo, Bar, Gnus):
pass

But when I try to validate the super-form using SuperSchema I can get
only fields defined in Foo, whilst the others are stripped. On the
other hand defining SuperSchema like this:

class SuperSchema(Foo, Bar, Gnus):
allow_extra_fields = True
filter_extra_fields = False
pass

works correctly, but leave me without protection from malicious fields
sent by the clients. Is there any other way to make this to work
correctly without disabling the extra fields filter?

Cheers,
Andrea
Ian Bicking
2009-03-05 17:48:57 UTC
Permalink
Another strategy you could use is:

class SuperSchema(Schema):
foo = Foo()
bar = Bar()
gnus = Gnus()

If Foo has a validator for, say, "email", then you need to name the
field "foo.email". I.e., every field starting with "foo." goes to
Foo, everything with "bar." goes to Bar, etc.

This is the intended way to reuse validation sets like this; multiple
subclasses are tricky and I can think of all too many reasons why they
wouldn't work.
Post by Andrea Riciputi
Hi,
I have a form composed of several fieldsets. Since I need to re-use
those fieldsets over and over again in many forms, I thought to write
a Schema object for each fieldset, and then combine them in a super-
    allow_extra_fields = True
    filter_extra_fields = True
    ... foo validators here ...
    pass
    allow_extra_fields = True
    filter_extra_fields = True
    ... bar validators here ...
    pass
    allow_extra_fields = True
    filter_extra_fields = True
    ... gnus validators here ...
    pass
    pass
But when I try to validate the super-form using SuperSchema I can get
only fields defined in Foo, whilst the others are stripped. On the
    allow_extra_fields = True
    filter_extra_fields = False
    pass
works correctly, but leave me without protection from malicious fields
sent by the clients. Is there any other way to make this to work
correctly without disabling the extra fields filter?
Cheers,
 Andrea
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Ian Bicking | http://blog.ianbicking.org
Loading...