How to make forms for Plone 3 the easy way
This How-to applies to:
Plone 3.0.x
This How-to is intended for:
Developers, Advanced Developers
plone.z3cform is a Plone package that allows you to easily use z3c.form in your Plone add-on.
Why you should consider using z3c.form
- Because Archetypes forms are hard to use independently of content types. This has led to several hacks in the past, where Archetypes content types were misused as tools, survey forms etc.
- Because zope.formlib is often too inflexible and hacky, due to its lack of adaptability and lack of a solid set of widgets.
I've been using z3c.form within Plone extensively for the Singing & Dancing newsletter add-on, and it's been like a breath of fresh air! No hackery, no getting in the way; it's a powerful set of tools for doing what you want to do: make forms.
What is plone.z3cform?
The plone.z3cform package is a very thin layer around z3c.form. Forms with plone.z3cform are essentially the same as pure Zope 3 forms built with z3c.form. No strings attached, and no special Zope 2/Plone hacks required (like self.context.aq_inner). z3c.form comes with excellent documentation, found in both the z3c.form source tree and online.
How do I use it?
plone.z3cform requires Plone 3.0 or higher, and the use of buildout.
Depending on z3c.form
plone.z3cform is available PyPI. If you already know how to depend on another package in your application, you can read ahead. A dependency is simply declared by adding the following to the install_requires section of your package's setup.py:
# ...
setup(
# ...
install_requires=[
'plone.z3cform',
'my.other.package',
],
# ...
)
In your application package's configure.zcml file you have to declare a dependency on the plone.z3cform configuration, like this:
<configure xmlns="http://namespaces.zope.org/zope">
<include package="plone.z3cform" />
...
</configure>
Using "fake eggs"
At the time of writing, plone.z3cform can only be used with buildout. This is because we're relying on the zope2install recipe's fake eggs feature to be able to depend on the latest and greatest Zope libraries available.
In your buildout configuration, look for the [zope2] section (the one that uses plone.recipe.zope2install). Then, add these lines to the section:
[zope2](You only need to add the bold lines here.) That's it.
recipe = plone.recipe.zope2install
url = ${plone:zope2-url}
fake-zope-eggs = true
skip-fake-eggs =
zope.testing
zope.component
zope.i18n
Examples
Tons of examples of using z3c.form can be found online. This is an example of a simple form for Plone:
>>> 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)
... ignoreContext = True # don't use context to get widget data
...
... @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"
Note that we're using base.FormWrapper as a base class for our browser view. We do this to wrap the form into the content area of a standard Plone page. This means that you can use your form class independently of layout in any part of your Plone site, e.g. in portlets or viewlets. You can register the MyView view just like any other browser:page. For more details on the base FormWrapper class, see the plone.z3cform.base module.
For more examples of forms within Plone, please refer to the project's homepage and see the Singing & Dancing packages.
If you have questions, don't hesitate to contact Plone's Add-on Product Developers list, or the authors of Singing & Dancing.
Re: PFG, PloneSurvey, etc
I hope this answers your question. I know nothing about PloneSurvey.
Re: Re: PFG ...
Thanx.
subforms
Re: subforms
Singing & Dancing has an example of using z3c.form's subforms with a Plone 3 style without hassle: http://dev.plone.org/collective/browser/collective.dancing/trunk/collective/dancing/browser/collector.py
overriding templates
1 <div tal:attributes="class view/css_class|nothing">
2 <h3 tal:replace="structure view/heading|nothing" />
3 <div tal:replace="structure view/contents_top|nothing" />
4 <metal:use use-macro="context/@@ploneform-macros/fields" />
5 <metal:use use-macro="context/@@ploneform-macros/actions" />
6 <div tal:replace="structure view/contents_bottom|nothing" />
7 </div>
PFG, PloneSurvey, etc
I've been using PloneFormGen and PloneSurvey for various use cases and I was wondering if there will be or if there should be, in your opinion, a convergence for general purpose forms generation in Plone to z3c.form.
Cheers,
Yves