[Zope3-Users] Using a multicheckboxwidget instead of a multiselectsetwidget

Jan-Wijbrand Kolman jw at infrae.com
Wed Jun 8 10:43:06 EDT 2005


Jan-Wijbrand Kolman wrote:
> I have a interface defined for some content type, say IFoo. IFoo defines one
> schema field:
> 
>   _filetypes = SimpleVocabulary.fromValues(('fropple', 'frepple', 'froople'))
> 
>   class IFoo(Interface):
> 
>     filetypes = Set(
>       title=u'',
>       required=False,
>       value_type=Choice(vocabulary=_filetypes),
>       )
> 
> By default, the edit widget that is used for this field, is a
> zope.app.form.browser.MultiSelectSetWidget, which will render a <select
> multiple="multiple">...</select> element.
> 
> Now I'd like to use multiple checkboxes instead, since the number of choices for
> this field will never exceed 3 or 4 anyway. I thought to accomplish this by
> configuring the MultiCheckBoxWidget for this, in the addform registration for
> the Foo content type:
> 
>   <addform
>     name="add_foo"
>     label="Add Foo"
>     schema=".interfaces.IFoo"
>     fields="filetypes"
>     content_factory=".foo.Foo"
>     template="foo.pt"
>     permission="zope.Public"
>   >
>   <widget
>     field="filetypes"
>     class="zope.app.form.browser.itemswidgets.MultiCheckBoxWidget"
>   />
>   </addform>
> 
> This however doesn't work and I get the following traceback (pasted the last
> couple of lines):
> 
>   File "...zope31/src/zope/app/form/browser/add.py", line 47, in _setUpWidgets
>     setUpWidgets(self, self.schema, IInputWidget, names=self.fieldNames)
>   File "...zope31/src/zope/app/form/utility.py", line 153, in setUpWidgets
>     context=context)
>   File "...zope31/src/zope/app/form/utility.py", line 101, in setUpWidget
>     widget = widget(field.bind(context), view.request)
>   File "...zope31/src/zope/app/form/__init__.py", line 97, in __call__
>     instance = self._widget_factory(*args)
> TypeError: __init__() takes exactly 4 arguments (3 given)
> 
> Now, I do indeed see the __init__ for the MultiCheckboxWidget class (well, one
> of its baseclasses actually) expects a vocabulary argument. How can I make sure
> this widget gets all the info it needs? Maybe I just don't see the obvious, but
> can anyone provide some hint in the right direction?

Answering myself. I have a workaround:

In mywidgets.py:

  from zope.app.form.browser import MultiCheckBoxWidget as MultiCheckBoxWidget_
  def MultiCheckBoxWidget(field, request):
    vocabulary = field.value_type.vocabulary
    return MultiCheckBoxWidget_(field, vocabulary, request)

In the configure.zcml:

  <addform
    name="add_foo"
    label="Add Foo"
    schema=".interfaces.IFoo"
    fields="filetypes"
    content_factory=".foo.Foo"
    template="foo.pt"
    permission="zope.Public"
  >
  <widget
    field="filetypes"
    class=".mywidgets.MultiCheckBoxWidget"
  />
  </addform>

Probably there're better ways to do this...?


kind regards,
jw

-- 
Jan-Wijbrand Kolman
jw at infrae.com


More information about the Zope3-users mailing list