带有两个选项卡的 Deform 2 / colander 模式甚至无法验证

Deform 2 / colander schema with two tabs does not even validate

正在尝试为 deform 2、colander 1.0 制作一个带有两个选项卡(命令式)的表单。表单的想法是在手动添加网页和标题之间进行选择,或者选择提要 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',))

在表单控制器(金字塔)中:

controls = request.POST.items()
...
appstruct = self.webpage_form.validate(controls)

虽然控件似乎有数据:

  [('_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')]

appstruct 为空:

  {'': {}}

试验了标题、缺失、默认、未知参数,没有结果。表格有什么问题?

此外,不是两个选项卡,而是两个相应的字段集,但这可能是另一回事。

问题解决了。 name 参数必须与 colander.SchemaNode 一起使用,而不是 title。生成的 appstruct 如下所示:

{'feed': {'feed_url': u''},
 'webpage': {'url': u'http://slashdot.org', 'webpage_name': u'Slashdot'}}

这是预期的结果。

再次阅读the docs很有帮助:

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.