Create a simple z3cform
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

<a href="http://www.patio-umbrellas.com">Patio Umbrellas</a>