Personal tools
You are here: Home Documentation How-tos How to make forms for Plone 3 the easy way
Support

Get Help

Join our chat rooms or support forums if you have more specific questions.

Plone Training
Learn how to design, build, and deploy a website in Plone through one of the numerous Plone training sessions around the world.
Find Plone training…
 
Document Actions

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

How to use z3c.form in Plone to create easy and flexible forms. Forget about complicated ways of making forms!

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]
recipe = plone.recipe.zope2install
url = ${plone:zope2-url}
fake-zope-eggs = true
skip-fake-eggs =
zope.testing
zope.component
zope.i18n

(You only need to add the bold lines here.)  That's it.

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.

by Daniel Nouri last modified June 24, 2008 - 07:58 All content is copyright Plone Foundation and the individual contributors.

PFG, PloneSurvey, etc

Posted by Yves Moisan at May 9, 2008 - 15:01
"z3c.form is the next generation Zope forms library"

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

Re: PFG, PloneSurvey, etc

Posted by Daniel Nouri at May 9, 2008 - 16:58
PloneFormGen is an excellent Product, but it has a different purpose compared to z3c.form; it's mostly user-oriented in that it allows the creation of forms and action handlers through the web. While z3c.form is a powerful tool aimed at developers. Of course, z3c.form could be *used* to develop something like PFG, which makes a lot of sense. But first of all, it's more low-level, and I hope that it will gradually replace all forms in Plone itself, like the ones based on Python Scripts, CMFFormController, AT, and formlib.

I hope this answers your question. I know nothing about PloneSurvey.

Re: Re: PFG ...

Posted by Yves Moisan at May 9, 2008 - 17:47
"z3c.form could be *used* to develop something like PFG" is what I suspected. I too very much like PFG. My guess is that PloneSurvey could also use z3c.form machinery, but then I'm a PloneSurvey user, not a developer.

Thanx.

subforms

Posted by Dylan Jay at May 18, 2008 - 16:05
It would be good to support forms which aren't whole pages or even subforms, yet still with the plone styling.

Re: subforms

Posted by Daniel Nouri at May 18, 2008 - 18:25
Dylan, this is already possible. The form in this how-to can be embedded anywhere by itself -- just put the HTML returned by MyForm(context, request)() wherever you want.

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

Posted by Dylan Jay at May 19, 2008 - 01:03
The fact that forms are always meant to provide their own template which then uses old fashioned macros such as subform.pt is what I was missing.

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>

Re: overriding templates

Posted by Daniel Nouri at May 19, 2008 - 07:40
Dylan, I changed this so that forms will always fall back to the default Plone style template if not specified differently. I made the change here: http://dev.plone.org/plone/changeset/20906

in case of "zope.component 0.0 has no such extra feature 'zcml'"

Posted by danielle d'avout at June 24, 2008 - 06:03
notice the change of the section fakezope2eggs(june 2008)

For any issues with the web site functionality, please file a ticket.

Please consult the policy on plone.org content if you want your content published on this site.

Servers and hosting by