gpt4 book ai didi

widget - z3c.form : add a css class to a widget

转载 作者:行者123 更新时间:2023-12-04 20:47:12 26 4
gpt4 key购买 nike

我正在用 z3c.form 编写一个表单,我想将一个 css 类添加到一个小部件。

所以我试图定义一个 updateWidgets 方法来添加 css 类,但是我在 super() 上遇到了这个错误:

2012-08-02 16:17:22 ERROR Zope.SiteErrorLog 1343917042.940.871101881907 http://pin:34180/Plone/alger/etape1
Traceback (innermost last):
Module ZPublisher.Publish, line 126, in publish
Module ZPublisher.mapply, line 77, in mapply
Module ZPublisher.Publish, line 46, in call_object
Module z3c.form.form, line 215, in __call__
Module z3c.form.form, line 208, in update
Module plone.z3cform.patch, line 21, in BaseForm_update
Module z3c.form.form, line 149, in update
Module bsw.inscription.browser.etape1, line 32, in updateWidgets
TypeError: super(type, obj): obj must be an instance or subtype of type

这是代码:

from zope import interface, schema
from z3c.form import form, field, button, validator
import zope.component

from bsw.inscription import inscriptionMessageFactory as _

class IEtape1Schema(interface.Interface):
# -*- extra stuff goes here -*-

valide_informations = schema.Bool(
title=u"J'assure avoir pris connaissance de toutes les informations ci-dessus",
description=u'',
required=True,
readonly=False,
default=False,
)


class Etape1(form.Form):

fields = field.Fields(IEtape1Schema)
ignoreContext = True

label = _(u'Etape 1')
description = _(u'')

def updateWidgets(self):
""" Customize widget options before rendering the form. """
super(Etape1, self).updateWidgets()
self.fields['valide_informations'].widget.addClass("myClass")

@button.buttonAndHandler(u'Submit')
def actionSubmit(self, action):
data, errors = self.extractData()
if errors:
self.status = self.formErrorsMessage
return
pass
# Put the action handler code here


class checkBoxValidator(validator.SimpleFieldValidator):
def validate(self, value):
if not value:
raise zope.interface.Invalid(_(u"Vous devez accepter les conditions"))

# Set conditions for which fields the validator class applies
validator.WidgetValidatorDiscriminators(checkBoxValidator, field=IEtape1Schema['valide_informations'])

# Register the validator so it will be looked up by z3c.form machinery

zope.component.provideAdapter(checkBoxValidator)

看了很多例子,不知道哪里错了。。。

最佳答案

因为要使 z3c.form 表单与 Plone 和主模板一起工作,需要进行大量重新包装,所以您的表单的类层次结构在注册时已更改。

结果,super()调用以这种形式中断,您需要直接调用未绑定(bind)的方法:

def updateWidgets(self):
""" Customize widget options before rendering the form. """
form.Form.updateWidgets(self)
self.fields['valide_informations'].widget.addClass("myClass")

关于widget - z3c.form : add a css class to a widget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11780251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com