Hi,

On Fri, May 12, 2006 at 10:48:52AM +0200, Marco Mariani wrote:
> 
> I've got a form which contains an object widget given by
> CustomWidgetFactory(ObjectWidget, MyClass)
> 
> Now, how can I render a Sequence widget to store a list of MyClass
> instances?

some months ago, Phillip von Weitershausen wrote such a widget for me. I'm
not using it anymore and I don't know, if it's still working. But maybe it
will help:




<configure.zcml>
        <!--
                A widget for rendering objects that are usually used
                in "lists of records" structures.
        -->
        <zope:view type="zope.publisher.interfaces.browser.IBrowserRequest"
                for="zope.schema.interfaces.IObject"
                provides="zope.app.form.interfaces.IInputWidget"
                factory=".widgets.ObjectInputWidget"
                permission="zope.Public" />
        <!--
                A template based widget for rendering edit forms out of 
sequences
                (eg. Lists) that are a bit more user friendly than the defaults.
        -->
        <zope:view type="zope.publisher.interfaces.browser.IBrowserRequest"
                for="zope.schema.interfaces.IList 
zope.schema.interfaces.IObject"
                provides="zope.app.form.interfaces.IInputWidget"
                factory=".widgets.TemplateSequenceWidget"
                permission="zope.Public" />
</configure.zcml>





<widgets.py>
from zope.app.form.browser import ObjectWidget, ListSequenceWidget
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
from zope.interface import implements
from zope.app import zapi
from zope.app.form.browser.objectwidget import ObjectWidgetView
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
from zope.app.form.browser.widget import BrowserWidget
from zope.app.form.interfaces import IDisplayWidget, IInputWidget
from zope.app.form import InputWidget
from zope.app.form.interfaces import WidgetInputError, MissingInputError
from zope.schema.interfaces import ValidationError, InvalidValue
from zope.app.i18n import MessageFactory
_=MessageFactory('mpgsite')
from zope.i18n import translate
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
from zope.app.form.browser.textwidgets import TextWidget
from zope.app.form.browser.widget import renderElement

def ObjectInputWidget(context, request):
        """Dispatch widget for Object schema field to widget that is
        registered for (Object, schema) where schema is the schema of the
        object."""
        class Obj(object):
                implements(context.schema)

        return zapi.getMultiAdapter((context, Obj(), request), IInputWidget)

class TemplateObjectWidget(ObjectWidget):
        """A Widget that uses a page template"""
        def __init__(self, context, request, factory, template_, **kw):
                super(TemplateObjectWidget, self).__init__(context, request, 
factory, **kw)
                class TemplateObjectWidgetView(ObjectWidgetView):
                        template = ViewPageTemplateFile(template_)
                self.view = TemplateObjectWidgetView(self, request)

class TemplateSequenceWidget(ListSequenceWidget):
        def __init__(self, context, field, request, 
template_='sequencewidget.pt', subwidget=None):
                super(TemplateSequenceWidget, self).__init__(
                    context, field, request, subwidget)
                class TemplateObjectWidgetView(ObjectWidgetView):
                        template = ViewPageTemplateFile(template_)
                self.view = TemplateObjectWidgetView(self, request)

        def __call__(self):
                return self.view()
</widgets.py>




<sequencewidget.pt>
<div tal:define="sequence   context/_getRenderedValue;
                 num_items  python:len(sequence);
                 min_length context/context/min_length;
                 max_length context/context/max_length"
     i18n:domain="mpgsite"
          tal:attributes="class context/classes"
        >
        <div class="itemstable">
        <div class="items">
  <div tal:repeat="i python:range(num_items)">
    <tal:block define="value  python:sequence[i];
                       widget python:context._getWidget(i);
                       nix    python:widget.setRenderedValue(value)">
        <input class="editcheck" type="checkbox"
               tal:attributes="name string:${context/name}.remove_${i}" />
        <tal:widget replace="structure widget" />
    </tal:block>
  </div>
  </div>
  </div>

  <input type="submit" value="Remove selected items"
                          tal:condition="python:sequence and num_items > 
min_length"
           tal:attributes="name string:${context/name}.remove"
           i18n:domain="zope"
           i18n:attributes="value remove-selected-items"/>

    <input type="submit" value="Add item"
           tal:attributes="name string:${context/name}.add"
                          tal:condition="python:max_length is None or num_items 
< max_length"
           i18n:attributes="value"
           i18n:domain="zope"/>

  <input type="hidden"
         tal:attributes="name  string:${context/name}.count;
                         value num_items" />

</div>
</sequencewidget.pt>



> I'm using formlib

Me, too :-)

Regards,

Frank
_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to