Current

This document is valid for the current version of Plone.

Create a simple z3cform

by Timo Stollenwerk last modified May 24, 2011 09:58 AM
Create a simple comment form.

First, we define a schema with a title, author, and text field for our comment form:

from zope import interface, schema

class IComment(interface.Interface):
title = schema.TextLine(title=u"Title")
author = schema.TextLine(title=u"Author",
required=False)
text = schema.TextLine(title=u"Text")

The comment form uses the schema definition to define and later render the form. Also we define a label that will show up above the form:

from z3c.form import form, field

class CommentForm(form.Form):
fields = field.Fields(IComment)
ignoreContext = True # don't use context to get widget data
label = "Add a comment"

Next, we add a submit button to the form with a method to handle the form data after we submitted the form. We extract the data from the form request, and return to the form if there are validation errors, otherwise we proceed:

from z3c.form import button
@button.buttonAndHandler(u'Post comment')
def handleApply(self, action):
data, errors = self.extractData()
if errors:
return
if data.has_key('text'):
print data['text'] # ... or do stuff

As last step, we have to wrap the form into a standard Plone page:

from plone.z3cform.layout import wrap_form
wrap_form(CommentForm)

If we put all these steps together, our comment.py file should look like this:

from zope import interface, schema
from z3c.form import form, field, button
from plone.z3cform.layout import wrap_form

class IComment(interface.Interface):
title = schema.TextLine(title=u"Title")
author = schema.TextLine(title=u"Author", required=False)
text = schema.TextLine(title=u"Text")

class CommentForm(form.Form):
fields = field.Fields(IComment)
ignoreContext = True # don't use context to get widget data
label = u"Add a comment"

@button.buttonAndHandler(u'Post comment')
def handleApply(self, action):
data, errors = self.extractData()
if data.has_key('title') and data.has_key('text'):
print data['title'] # ... or do stuff

CommentView = wrap_form(CommentForm)

For more z3c.form schema details see http://docs.zope.org/z3c.form/browser/README.html.

The only thing left to do in order to use our form is to register it in the configure.zcml:

<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:five="http://namespaces.zope.org/five"
xmlns:browser="http://namespaces.zope.org/browser"
i18n_domain="example.z3cformtutorial">

<!-- Include z3c.form as dependency -->
<include package="plone.app.z3cform" />

<!-- Register the comment form -->
<browser:page
for="Products.CMFPlone.interfaces.IPloneSiteRoot"
name="comment_form"
class=".comment.CommentView"
permission="zope2.View"
/>

</configure>

Start the Zope instance

$ ./bin/instance fg

Go to the ZMI and create a Plone instance with the name 'test' and the Generic Setup profile "Plone z3c.form support". Then, go to the following URL

http://localhost:8080/test/comment_form

Comments (5)

kevin anchi Jan 28, 2011 05:54 PM
Thanks
<a href="http://www.patio-umbrellas.com">Patio Umbrellas</a>
kevin anchi May 24, 2011 09:55 AM
    Adjust test for the contentprovider feature to not depend on the ContentProviderBase class that was introduced in zope.contentprovider 3.5.0. This restores compatibility with Zope 2.10.
    Security issue, removed IBrowserRequest from IFormLayer. This prevents to mixin IBrowserRequest into non IBrowserRequest e.g. IJSONRPCRequest. This should be compatible since a browser request using z3c.form already provides IBrowserRequest and the IFormLayer is only a marker interface used as skin layer.
    Add English translation (generated from translation template using msgen z3c.form.pot > en/LC_MESSAGES/z3c.form.po).
    Added Norwegian translation, thanks to Helge Tesdal and Martijn Pieters.
    Updated German translation.

http://www.bestpanicattackc[…]l-cure-pure-calm-review.htm
Ralph Jacobs Mar 29, 2011 01:06 PM
In the second example code box the 'class' is missing.
Ralph Jacobs Mar 29, 2011 01:08 PM
In the third example code box are two decorators: @button.buttonAndHandler(u'Post comment')
Timo Stollenwerk May 24, 2011 10:00 AM
Fixed. Thanks for the hints!

Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.