Plone: Forms finally made easy: z3c.forms integration to Plone using plone.z3cform
Forms using zope.formlib are a PITA to do (don't even mention CMF Form Controller). zc3.forms and plone.z3c.forms come to a rescue.
Basic Example
--------------
::
>>> from zope import interface, schema
>>> from z3c.form import form, field, button
>>> from plone.z3cform import base
>>> class MySchema(interface.Interface):
... age = schema.Int(title=u"Age")
>>> class MyForm(form.Form):
... fields = field.Fields(MySchema)
...
... @button.buttonAndHandler(u'Apply')
... def handleApply(self, action):
... data, errors = self.extractData()
... print data['age'] # ... or do stuff
>>> class MyView(base.FormWrapper):
... form = MyForm
... label = u"Please enter your age"
Simple CRUD form (CReate Update Delete)
---------------------------------------
First, let's define an interface and a class to play with::
>>> from zope import interface, schema
>>> class IPerson(interface.Interface) :
... name = schema.TextLine()
... age = schema.Int()
>>> class Person(object):
... interface.implements(IPerson)
... def __init__(self, name=None, age=None):
... self.name, self.age = name, age
... def __repr__(self):
... return "" % (self.name, self.age)
For this test, we take the the name of our persons as keys in our storage::
>>> storage = {'Peter': Person(u'Peter', 16),
... 'Martha': Person(u'Martha', 32)}
Our simple form looks like this::
>>> class MyForm(crud.CrudForm):
... update_schema = IPerson
...
... def get_items(self):
... return sorted(storage.items(), key=lambda x: x[1].name)
...
... def add(self, data):
... person = Person(**data)
... storage[str(person.name)] = person
... return person
...
... def remove(self, (id, item)):
... del storage[id]
This is all that we need to render a combined edit add form containing all our items::
>>> from z3c.form.testing import TestRequest
>>> print MyForm(None, TestRequest())() \
... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
...Martha...Peter...
Link
----
http://pypi.python.org/pypi/plone.z3cform

Previous:
Plone: High-Performance Indexing using an external non-Zope indexer
