Package: jyu.formwidget.object
schema.Object made simpleThis package introduces an experimental proof-of-concept support for
object-fields (zope.schema.Object) on z3c.form and Plone for a
use-case where one desires to save schema based hierarchical data on
objects.I can’t say, why anyone would like to do that (instead of mapping data
to containers and items), but I hope this package provides examples,
how to make object-fields work withplone.autoformand Plone’s
KSS-validation.This packageprovidesISubformFactoryfor object-widget withinIAutoExtensibleFormimplements simple Plone-styleinputanddisplaywidgets for object-fieldintroduces refactored KSS-validation integration with support for object-fieldmonkeypatchesplone.z3cform's widget traversal to support object-widgetsoverrides defaultDataConverterforObjectWidget's with a less invasive oneprovides a simple abstract factory class to store object-fields’
values asSimpleItem-properties.Note that this package relies onplone.app.z3cformandIPloneFormLayerit registers.Example of UseAt first we define a simple schema we’d like to re-use as a part of other schemas:from zope import schema
from zope.interface import invariant, Invalid

from plone.directives import form

from zope.i18nmessageid import MessageFactory as ZopeMessageFactory
_= ZopeMessageFactory("my.package")


class StartBeforeEnd(Invalid):
    __doc__ = _(u"The start or end date is invalid")


class IPeriod(form.Schema):
    start = schema.Date(
        title=_(u"period_start_label",
                default=u"Period began"),
        required=True
        )

    end = schema.Date(
        title=_(u"period_end_label",
                default=u"Period ended"),
        required=True
        )

    @invariant
    def validateStartEnd(data):
        if data.start is not None and data.end is not None:
            if data.start > data.end:
                raise StartBeforeEnd(\
                    _(u"The start date must be before the end date."))Then we define the main schema, which re-uses the first schema:class IWorkPeriod(form.Schema):
    title = schema.TextLine(
        title=_(u"work_title_label",
                default=u"Title"),
        required=True
        )
    description = schema.TextLine(
        title=_(u"work_description_label",
                default=u"Description"),
        required=False
        )
    period = schema.Object(
        title=_(u"work_period",
                default=u"Period"),
        schema=IPeriod,
        required=True
        )Finally, we register an object factor, which createsSimpleItemmatching our schema forz3c.formto validate and store as a
property of the actual object being created or edited:from five import grok

from zope.interface import Interface

from z3c.form.interfaces import IObjectFactory

from jyu.formwidget.object.factory import AbstractBaseFactory

from my.package.schemas import IPeriod


class PeriodFactory(AbstractBaseFactory, grok.MultiAdapter):
    grok.provides(IObjectFactory)
    grok.name("my.package.schemas.IPeriod")
    grok.adapts(Interface, Interface, Interface, Interface)To be able to test this, you should, of course, also define and
register a new content type based on the main schema (e.g. with
Dexterity).Changelog1.0b7 - 2011-02-23Fixed datamanager to work as attribute field by default to
fix problems withplone.app.textfieldandplone.namedfile.1.0b6 - 2011-01-28Added note about problems ofplone.app.textfieldandplone.namedfile.1.0b5 - 2011-01-24Merged changes fromplone.app.z3form(0.5.3) tovalidation.py.1.0b4 - 2011-01-19Fixed datamanager to initialize object attribute also when it
already exists with None.1.0b3 - 2011-01-07Fixed validator and data converter to handle sub object field.1.0b2 - 2010-12-23Refactored to use custom DataConverter and DataManager instead of
the one shipped withz3c.form.1.0b1 - 2010-12-20Initial release