Trying to make a form with two tabs (in imperative style) for deform 2, colander 1.0. The idea of the form is to choose between adding webpage and it's title manually, or alternatively a feed URL:
@property
def webpage_form(self):
schema = colander.SchemaNode(colander.Mapping(unknown='preserve'),
name="webpage_schema")
schema_page = colander.SchemaNode(colander.Mapping(unknown='preserve'),
title=u"Webpage", missing={})
webpage_name = colander.SchemaNode(
colander.String(),
name='webpage_name',
default=u'Webpage',
missing=u'',
)
webpage_url = colander.SchemaNode(
colander.String(),
name='url',
default=u'-',
missing=u'',
)
schema_page.add(webpage_name)
schema_page.add(webpage_url)
schema_feed = colander.SchemaNode(colander.Mapping(unknown='preserve'),
title=u"Feed", missing={})
feed = colander.SchemaNode(
colander.String(),
name='feed_url',
default=u'-',
missing=u'',
)
schema_feed.add(feed)
schema.add(schema_page)
schema.add(schema_feed)
return deform.Form(schema, buttons=('submit',))
In the form controller (Pyramid):
controls = request.POST.items()
...
appstruct = self.webpage_form.validate(controls)
While the controls seems to have data:
[('_charset_', u'UTF-8'),
('__formid__', u'deform'),
('__start__', u':mapping'),
('webpage_name', u'Webpage'),
('url', u'http://slashdot.org'),
('__end__', u':mapping'),
('__start__', u':mapping'),
('feed_url', u'-'),
('__end__', u':mapping'),
('submit', u'submit')]
The appstruct is empty:
{'': {}}
Experimented with title, missing, default, unknown parameters, without result. What is wrong with the form?
Also, instead of two tabs there are two consequent fieldsets, but it may be another story.
Solved the problem. The name
parameter must be used with colander.SchemaNode
, not title
. The resulting appstruct then looks like this:
{'feed': {'feed_url': u''},
'webpage': {'url': u'http://slashdot.org', 'webpage_name': u'Slashdot'}}
which is what was expected.
It helped to read the docs again:
Each schema node object has a required type, an optional preparer for adjusting data after deserialization, an optional validator for deserialized prepared data, an optional default, an optional missing, an optional title, an optional description, and a slightly less optional name.