首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在z3c.form中生成所需的布尔字段?

如何在z3c.form中生成所需的布尔字段?
EN

Stack Overflow用户
提问于 2012-03-12 16:21:33
回答 2查看 1.1K关注 0票数 4

我正在使用z3c.form在plone4.1.4中创建一个表单。我需要一个需要的布尔字段:用户必须勾选该框。(在我的情况下,用户必须同意条款和条件。)

对字段使用required=True不起作用:我可以提交表单而不检查复选框。

这就是我的代码的样子:

代码语言:javascript
复制
from five import grok
from plone.directives import form
from zope import schema
from z3c.form import button


from Products.CMFCore.interfaces import ISiteRoot
from Products.statusmessages.interfaces import IStatusMessage


class ITestSchema(form.Schema):
    hasApprovedConditions = schema.Bool(
        title=u'I agree to the Terms and Conditions.',
        required=True,
    )


class TestForm(form.SchemaForm):
    grok.name('test-form')
    grok.require('zope2.View')
    grok.context(ISiteRoot)

    schema = ITestSchema
    ignoreContext = True

    @button.buttonAndHandler(u'Send')
    def handleApply(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return

        IStatusMessage(self.request).addStatusMessage(u'Thanks', 'info')
        self.request.response.redirect(self.context.absolute_url())

表单显示了复选框和标签,但没有迹象表明该字段是必需的,实际上它不是:我可以提交表单,而不需要勾选复选框。

我正在扩展这些已知的好集合:

  • http://dist.plone.org/release/4.1.4/versions.cfg
  • http://good-py.appspot.com/release/dexterity/1.2?plone=4.1.4

他们将z3c.form与2.5.1版本联系在一起,但我也尝试了2.6.1版本。

我遗漏了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-12 16:36:46

您应该使用如下约束:

代码语言:javascript
复制
def validateAccept(value):
    if not value == True:
        return False
    return True

class ITestSchema(form.Schema):
    hasApprovedConditions = schema.Bool(
        title=u'I agree to the Terms and Conditions.',
        required=True,
        constraint=validateAccept,
    )

更多信息

  • http://plone.org/products/collective.examples.userdata
票数 9
EN

Stack Overflow用户

发布于 2013-07-23 18:11:58

要对“缺陷”做出回应,请注意@-只需添加一个:

代码语言:javascript
复制
description=_(u'Required'),
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9670819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档