简体   繁体   English

在z3c.form列表中预勾选特定的复选框

[英]Pre-tick specific checkbox in z3c.form list

I have a z3c.form that's used during registration. 我有一个在注册过程中使用的z3c.form。 One of the fields is a list of emails users may wish to sign up for. 字段之一是用户可能希望注册的电子邮件列表。

from zope import schema
from zope.schema.vocabulary import SimpleVocabulary

emailVocab = SimpleVocabulary.fromItems((
    ('sysn', u'System notifications (strongly recommended)'),
    ('mark', u'Marketing emails'),
    ('offe', u'Special offers')))

...

email_optin = schema.List(
    title = u'',
    description = u'',
    required = False,
    value_type = schema.Choice(source=emailVocab))

I'd like to have the first of these to be selected by default, while the others should not be. 我想默认选择其中的第一个,而其他则不应该。 I can't see a way in the spec to do this. 我看不到规范中执行此操作的方法。 Any ideas? 有任何想法吗?

Simplest case is as documented in Modelling using zope.schema, default value section , which z3c.form picks up ( relevant documentation ). 最简单的情况如“ 使用zope.schema进行建模”(默认值部分 )中所述, z3c.form了介绍( 相关文档 )。 However, this is complicated by the fact that the default values should not be mutable as the instance is shared across everything, so for safety sake a defaultFactory argument is implemented for handling this. 但是,由于实例之间的所有对象都共享默认值,因此默认值不应是可变的,这使情况变得复杂,因此为了安全起见,实现了defaultFactory参数来处理此情况。 Putting all that together, you should have something like this: 将所有内容放在一起,您应该具有以下内容:

import zope.schema
import zope.interface
from zope.schema.vocabulary import SimpleVocabulary

emailVocab = SimpleVocabulary.fromItems((
    ('sysn', u'System notifications (strongly recommended)'),
    ('mark', u'Marketing emails'),
    ('offe', u'Special offers')))

def default_email():
    return [u'Special offers']  # example


class IEmailPreference(zope.interface.Interface):

    # ...

    email_optin = zope.schema.List(
        title=u'',
        description=u'',
        required=False,
        value_type=zope.schema.Choice(source=emailVocab),
        defaultFactory=default_email,
    )

Do note that the actual value used for the default is not the token part but the value part, hence the string 'Special offers' is returned instead of 'offe' . 请注意,用于默认值的实际值不是token部分,而是value部分,因此将返回字符串'Special offers' 'offe'而不是'offe' It is documented in the documentation about Vocabularies . 有关词汇的文档中对此进行了记录 If the human readable part is intended to be the title and you wish the actual value to be the same as the token you will need to adjust your code accordingly. 如果人类可读的部分打算用作标题,并且您希望实际值与令牌相同,则需要相应地调整代码。 Otherwise, to select the first one you simply have default_email return [u'System notifications (strongly recommended)'] . 否则,要选择第一个,只需让default_email返回[u'System notifications (strongly recommended)']

For completeness, your form module might look something like this: 为了完整起见,您的表单模块可能看起来像这样:

from z3c.form.browser.checkbox import CheckBoxFieldWidget
from z3c.form.form import Form

class EmailPreferenceForm(Form):

    fields = z3c.form.field.Fields(IEmailPreference)
    fields['email_optin'].widgetFactory = CheckBoxFieldWidget

Alternatively, you can use value discriminators to approach this issue if you don't wish to populate the interface with a default value or factory for that, but this is a lot more effort to set up so I generally avoid dealing with that when this is good enough. 另外,如果您不希望为接口填充默认值或工厂,则可以使用值区分符来解决此问题,但这要花费更多的精力来设置,因此通常避免在此情况下处理该问题。够好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM