A collection of widgets, templates and other components for use with z3c.form and Plone
Project description
A collection of widgets and templates, and other components for use with z3c.form in Plone. This extends plone.z3cform, the library that enables Zope 2 applications to use z3c.form, with Plone-specific markup and functionality.
Please read the documentation for z3c.form, which contains important information about using z3c.form in Zope 2 in general. For the most part, that package contains the “active” parts that you need to know about, and this package provides “passive” overrides that make the forms integrate with Plone.
Installation
To use z3c.form forms in Plone, you should install this package. First, depend on it in your own package’s setup.py, using the install_requires list. Then load its configuration form your own package’s configure.zcml, with:
<include package="plone.app.z3cform" />
Before you can use the forms, you also need to install the plone.app.z3cform:default GenericSetup extension profile. The best way to do that is to install it as a dependency of your own product’s installation profile. In your metadata.xml, add a dependency like:
<metadata> ... <dependencies> ... <dependency>profile-plone.app.z3cform:default</dependency> </dependencies> </metadata>
Note that if you don’t install the product, and you are using standalone z3c.form forms (in Zope 2.12 or later), you will find that z3c.form complains about missing widgets. This is because the IFormLayer marker interface has not been applied to the request.
In fact, the browser layer installed with this product’s extension profile is plone.app.z3cform.interfaces.IPloneFormLayer, which in turn derives from z3c.form.interfaces.IFormLayer.
Default macros
This package overrides the @@ploneform-macros view from plone.z3cform, using standard Plone markup for form fields, fieldsets, etc.
All the macros described in plone.z3cform are still available. In addition, you can use the widget_rendering macro to render all the default widgets, but none of the fieldsets (groups) or the fieldset headers (which would be rendered with the fields macro).
Each widget is rendered using the @@ploneform-render-widget view, which by default includes the widget’s label, required indicator, description, errors, and the result of widget.render(). This view may be overridden for particular widget types in order to customize this widget chrome.
Inline form validation
This package installs AJAX handlers to perform inline field validation. On any form, the field will be validated when the user blurs a field.
This relies on the KSS framework, and is only installed if plone.app.kss is available. If you are using a custom form, note that you must define the following “kassattr” variables:
formname, the name of the form view, defined on the <form /> element.
fieldname, the name of the current field (same as the widget name), defined on an element wrapping the field.
fieldset, defined for non-default fieldsets on the <fieldset /> element.
This also assumes the standard Plone form markup is used. See templaes/macros.pt for details.
Template enhancements
The following apply in plone.app.z3cform templates which are defined in plone.app.z3cform/plone/app/z3cform/templates/macros.pt. They allow you to customize the behavior of z3c.form package to play nicely with your application.
plone.app.z3cform add-on must be installed through the add on installer on your site, or plone.app.z3cform form macros are not activated. Running the installer adds a custom browser layer where macros.pt is hooked as ploneform-macros view.
Form method
If your form instance defines a property called method it allows you to set whether form is HTTP POST or HTTP GET. The default is POST. This translates to <form method="post"> attribute.
Example:
class HolidayServiceSearchForm(form.Form): """ Example search form of which results can be bookmarked. Bookmarking is possible because we use HTTP GET method. """ method = "get"
Form action
Form action property defines HTTP target where the form is posted. The default is the same page where the form was rendered, request.getURL().
Example:
class HolidayServiceSearchForm(form.Form): def action(self): """ Redefine <form action=''> attribute. We use URL fragment to define the <a> anchor were we directly scroll at the results when the form is posted, skipping unnecessary form fields part. The user can scroll back there if he/she wants modify the parameters. """ # Context item URL + form view name + link fragment. # This works for HTTP GET forms only. # Note that we cannot use request.getURL() as it might contain # 1) prior fragment 2) GET query parameters messing up the UrL return self.context.absolute_url() + "/holidayservice_view" + "#searched"
Troubleshooting
Here are some common errors you might encounter with plone.app.z3cform.
ComponentLookupError in updateWidgets()
Traceback (innermost last): Module ZPublisher.Publish, line 119, in publish Module ZPublisher.mapply, line 88, in mapply Module ZPublisher.Publish, line 42, in call_object Module plone.z3cform.layout, line 64, in __call__ Module plone.z3cform.layout, line 54, in update Module getpaid.expercash.browser.views, line 63, in update Module z3c.form.form, line 208, in update Module z3c.form.form, line 149, in update Module z3c.form.form, line 128, in updateWidgets Module zope.component._api, line 103, in getMultiAdapter ComponentLookupError: ((<getpaid.expercash.browser.views.CheckoutForm object at 0xdb052ac>, <HTTPRequest, URL=http://localhost:8080/test/@@getpaid-checkout-wizard>, <PloneSite at /test>), <InterfaceClass z3c.form.interfaces.IWidgets>, u'')
plone.app.z3cform layers are not in place (configuration ZCML is not read). You probably forgot to include plone.app.z3cform in your product’s configuration.zcml. See Installation above.
WYSIWYG widget
The plone.app.z3cform.wysiwyg package provides an implementation of the Plone WYSIWYG widget compatible with z3c.form. This will allow you to use Kupu, FCKeditor and other editors compatible with the Plone WYSIWYG interface in your z3c.form forms.
To use, simply set the widget factory for the widget you’d like to be displayed with the WYSIWYG widget:
>>> from zope import interface, schema >>> from z3c.form import form, field >>> from z3c.form.interfaces import INPUT_MODE >>> from plone.app.z3cform.wysiwyg.widget import WysiwygFieldWidget>>> class IProfile(interface.Interface): ... name = schema.TextLine(title=u"Name") ... age = schema.Int(title=u"Age") ... bio = schema.Text(title=u"Bio")>>> class MyForm(form.Form): ... fields = field.Fields(IProfile) ... fields['bio'].widgetFactory[INPUT_MODE] = WysiwygFieldWidget
Query select widget
The plone.app.z3cform.queryselect module provides a query source compatible with z3c.formwidget.query which combines to a selection field that can be queried.
The native value type for the widget is Archetypes UID collections. The default implementation will simply search using the SearchableText index in the portal catalog.
This is how your form schema could look like:
>>> from zope import interface, schema >>> from plone.app.z3cform.queryselect import ArchetypesContentSourceBinder>>> class ISelection(interface.Interface): ... items = schema.Set( ... title=u"Selection", ... description=u"Search for content", ... value_type=schema.Choice( ... source=ArchetypesContentSourceBinder()))
Optionally, instead of storing Archetypes UIDs, you can choose to use persistent.wref, i.e. weak references, instead of UIDs:
>>> from plone.app.z3cform.queryselect import uid2wref >>> factory = uid2wref(ISelection['items'])
To store weak references instead of UIDs you would register such a factory as a component adapting the context. The factory automatically provides the interface which defines the field.
KSS inline validation
First, let’s set up KSS debug mode:
>>> from zope.interface import alsoProvides >>> from Testing.ZopeTestCase import ZopeLite >>> from Testing.makerequest import makerequest >>> from kss.core.tests.base import IDebugRequest >>> from zope.annotation.interfaces import IAttributeAnnotatable>>> app = ZopeLite.app() >>> def make_request(form={}, lang='en'): ... request = makerequest(app, environ = {'HTTP_ACCEPT_LANGUAGE': lang}).REQUEST ... request.form.update(form) ... alsoProvides(request, IDebugRequest) ... alsoProvides(request, IAttributeAnnotatable) ... return request
Then we create a simple z3c form
>>> from zope import interface, schema >>> from z3c.form import form, field, button >>> from plone.app.z3cform.layout import FormWrapper>>> 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 MyFormWrapper(FormWrapper): ... form = MyForm ... label = u"Please enter your age">>> from zope.component import provideAdapter >>> from zope.publisher.interfaces.browser import IBrowserRequest >>> from zope.interface import Interface>>> provideAdapter(adapts=(Interface, IBrowserRequest), ... provides=Interface, ... factory=MyFormWrapper, ... name=u"test-form")
Let’s verify that worked:
>>> from zope.component import getMultiAdapter >>> from zope.interface import Interface, implements >>> from Acquisition import Implicit >>> class Bar(Implicit): ... implements(Interface) ... def restrictedTraverse(self, name): ... # fake traversal to the form ... if name.startswith('@@'): ... return getMultiAdapter((self, self._REQUEST), Interface, name[2:]).__of__(self) ... else: ... return getattr(self, name) ... >>> context = Bar() >>> request = make_request() >>> context._REQUEST = request # evil test fake >>> formWrapper = getMultiAdapter((context, request), name=u"test-form") >>> formWrapper <Products.Five.metaclass.MyFormWrapper object ...> >>> formWrapper.form <class 'plone.app.z3cform.tests.example.MyForm'>>>> del context, request
Inline validation
Inline validation is invoked via the @@kss_z3cform_inline_validation view.
>>> context = Bar() >>> request = make_request(form={'form.widgets.age': 'Title'}) >>> context._REQUEST = request >>> view = getMultiAdapter((context, request), name=u"kss_z3cform_inline_validation")
This is wired up with KSS. When the user leaves a form control with inline validation enabled, it will be called with the following parameters:
>>> view.validate_input(formname=u'test-form', fieldname=u'form.widgets.age', value='Title') [{'selectorType': 'css', 'params': {}, 'name': 'clearChildNodes', 'selector': u'#formfield-form-widgets-age div.fieldErrorBox'}, {'selectorType': 'css', 'params': {'value': u'error warning'}, 'name': 'addClass', 'selector': u'#formfield-form-widgets-age'}]>>> request = make_request(form={'form.widgets.age': '20'}) >>> context._REQUEST = request >>> view = getMultiAdapter((context, request), name=u"kss_z3cform_inline_validation") >>> view.validate_input(formname=u'test-form', fieldname=u'form.widgets.age', value='20') [{'selectorType': 'css', 'params': {}, 'name': 'clearChildNodes', 'selector': u'#formfield-form-widgets-age div.fieldErrorBox'}, {'selectorType': 'css', 'params': {'value': u'error'}, 'name': 'removeClass', 'selector': u'#formfield-form-widgets-age'}, {'selectorType': 'css', 'params': {'value': u'warning'}, 'name': 'removeClass', 'selector': u'#formfield-form-widgets-age'}, {'selectorType': 'css', 'params': {'name': u'display', 'value': u'none'}, 'name': 'setStyle', 'selector': '.portalMessage'}, {'selectorType': 'htmlid', 'params': {'html': u'<![CDATA[<dt>Info</dt><dd></dd>]]>', 'withKssSetup': u'True'}, 'name': 'replaceInnerHTML', 'selector': 'kssPortalMessage'}, {'selectorType': 'htmlid', 'params': {'name': u'class', 'value': u'portalMessage info'}, 'name': 'setAttribute', 'selector': 'kssPortalMessage'}, {'selectorType': 'htmlid', 'params': {'name': u'display', 'value': u'none'}, 'name': 'setStyle', 'selector': 'kssPortalMessage'}]
Inline validation with groups
We use plone.app.z3cform.tests.example.MyGroupFormWrapper and validate the field ‘name’ that’s part of a group. Inline validation is invoked via the @@kss_z3cform_inline_validation view.
>>> request = make_request(form={'form.widgets.name': ''}) >>> context._REQUEST = request >>> view = getMultiAdapter((context, request), name=u"kss_z3cform_inline_validation")
The validation view takes an Attribute fieldset with the index of the group.
>>> view.validate_input(formname=u'test-group-form', fieldname=u'form.widgets.name', fieldset="0", value='') [{'selectorType': 'css', 'params': {}, 'name': 'clearChildNodes', 'selector': u'#fieldset-0 #formfield-form-widgets-name div.fieldErrorBox'}, {'selectorType': 'css', 'params': {'value': u'error warning'}, 'name': 'addClass', 'selector': u'#fieldset-0 #formfield-form-widgets-name'}]>>> request = make_request(form={'form.widgets.name': u'Name'}) >>> context._REQUEST = request >>> view = getMultiAdapter((context, request), name=u"kss_z3cform_inline_validation") >>> view.validate_input(formname=u'test-group-form', fieldname=u'form.widgets.name', fieldset="0", value=u'Name') [{'selectorType': 'css', 'params': {}, 'name': 'clearChildNodes', 'selector': u'#fieldset-0 #formfield-form-widgets-name div.fieldErrorBox'}, {'selectorType': 'css', 'params': {'value': u'error'}, 'name': 'removeClass', 'selector': u'#fieldset-0 #formfield-form-widgets-name'}, {'selectorType': 'css', 'params': {'value': u'warning'}, 'name': 'removeClass', 'selector': u'#fieldset-0 #formfield-form-widgets-name'}, {'selectorType': 'css', 'params': {'name': u'display', 'value': u'none'}, 'name': 'setStyle', 'selector': '.portalMessage'}, {'selectorType': 'htmlid', 'params': {'html': u'<![CDATA[<dt>Info</dt><dd></dd>]]>', 'withKssSetup': u'True'}, 'name': 'replaceInnerHTML', 'selector': 'kssPortalMessage'}, {'selectorType': 'htmlid', 'params': {'name': u'class', 'value': u'portalMessage info'}, 'name': 'setAttribute', 'selector': 'kssPortalMessage'}, {'selectorType': 'htmlid', 'params': {'name': u'display', 'value': u'none'}, 'name': 'setStyle', 'selector': 'kssPortalMessage'}]
Inline-Validation and Translation of ErrorSnippets
We use plone.app.z3cform.tests.example.MyGroupFormWrapper and validate the field ‘name’ that’s part of a group. Inline validation is invoked via the @@kss_z3cform_inline_validation view.
>>> request = make_request(form={'form.widgets.name': ''}, lang='de',) >>> context._REQUEST = request >>> view = getMultiAdapter((context, request), name=u"kss_z3cform_inline_validation")
The validation view takes an Attribute fieldset with the index of the group. The error is only shown when warning_only is explicitly switched off (matching the behavior of archetypes.)
>>> view.validate_input(formname=u'test-group-form', fieldname=u'form.widgets.name', fieldset="0", value='', warning_only=False) [{'selectorType': 'css', 'params': {'html': u'<![CDATA[Erforderliche Eingabe fehlt.]]>', 'withKssSetup': u'True'}, 'name': 'replaceInnerHTML', 'selector': u'#fieldset-0 #formfield-form-widgets-name div.fieldErrorBox'}, {'selectorType': 'css', 'params': {'value': u'error'}, 'name': 'addClass', 'selector': u'#fieldset-0 #formfield-form-widgets-name'}]
Forms embedded inside normal views
It’s possible to embed z3c.form Forms inside a normal BrowserView via viewlets, portlets or tiles.
Currently the name of the form to be validated is gotten from the URL. For embedded forms this can’t work since the URL only has the containing view’s name.
Until a lasting solution is found, we just make sure that validation doesn’t raise an exception if it receives a normal browerview as the supposed form.
>>> from zope.publisher.browser import BrowserView >>> class MyNormalView(BrowserView): ... """ """>>> provideAdapter(adapts=(Interface, IBrowserRequest), ... provides=Interface, ... factory=MyNormalView, ... name=u"my-view")
Let’s verify that it gets called…
>>> context = Bar() >>> request = make_request() >>> view = getMultiAdapter((context, request), name=u"my-view") >>> view <MyNormalView object ...>
Inline validation is invoked via the @@kss_z3cform_inline_validation view. But in this case no validation output should be returned.
>>> context = Bar() >>> request = make_request(form={'form.widgets.age': 'Title'}) >>> view = getMultiAdapter((context, request), name=u"kss_z3cform_inline_validation") >>> view.validate_input(formname=u'my-view', fieldname=u'form.widgets.age', value='Title') []
Changelog
0.6.2 (2012-12-09)
Rename the ‘fieldset.current’ hidden input to ‘fieldset’ for consistency with Archetypes. [davisagli]
Fix a case where the widget broke if its form’s content was a dict. [davisagli]
0.6.1 (2012-08-30)
Fix the single checkbox widget to cope with widgets with a __call__ method. [davisagli]
0.6.0 (2012-05-25)
Remove hard-coded ■ (box) markers from required labels to match changes made in sunburst/public.css and archetypes. Fixes double required markers in Plone 4.2rc1.
Pull form help inside label tag and make it a span rather than a div. The purpose is to improve accessibility by making the semantic connection between label and help. Related to http://dev.plone.org/ticket/7212
Use ViewPageTemplateFile from zope.browserpage. [hannosch]
0.5.8 (2012-05-07)
Prevent empty error divs from being generated if errors are already associated with a field. [davidjb]
0.5.7 - 2011-11-26
Corrected formatting for errors on the FieldWidgets object (i.e. from invariants). This closes http://code.google.com/p/dexterity/issues/detail?id=238 [davisagli]
Added the i18n:domain attribute in the first div of widget.pt in order to make the “required” tooltip translatable. Fixes http://dev.plone.org/plone/ticket/12209 [rafaelbco]
0.5.6 - 2011-06-30
Make sure group errors get styled like field errors. [davisagli]
Include group and field descriptions as structure. [davisagli]
0.5.5 - 2011-06-26
Make it possible to add a custom CSS class to a form by setting its css_class attribute. [davisagli]
Match plone.z3cform’s template in including the form description as structure. [davisagli]
0.5.4 - 2011-05-04
Customize templates for multi and object widgets for more consistent styling. [elro]
Remove dependency on zope.app.component. [davisagli]
Add MANIFEST.in. [WouterVH]
Raise LookupError when terms are not found (e.g. they are no longer visible due to security) [lentinj]
0.5.3 - 2011-01-22
Fix test setup in Zope 2.10. [davisagli]
0.5.2 - 2011-01-18
Don’t use collective.testcaselayer based IntegrationTestLayer as it leads to PicklingError on Plone 4.1. [elro]
Change inline validation to match archetypes behavior - add a warning class and omit the error message. [elro]
0.5.1 - 2010-11-02
Make sure form.extractData() does not raise an AttributeError if the method is called before the form is available (first page load). [timo]
This package now uses the plone i18n domain. [vincentfretin]
Added option to override <form action=””>. [miohtama]
Updated README regarding form action and method. [miohtama]
0.5.0 - 2010-04-20
Render errors from group form widget manager validators. Fixes http://code.google.com/p/dexterity/issues/detail?id=96 [davisagli]
Default to showing the default fieldset, rather than the first non-default fieldset. [davisagli]
Replace the required field indicator image with a unicode box, refs http://dev.plone.org/plone/ticket/10352 [davisagli, limi]
Replaced the existing radiobutton-based boolean widget with the standard single checkbox Plone version. [limi]
Add @@ploneform-render-widget view, so that the widget chrome can be customized for particular widget types. [davisagli]
Added slots to the titlelessform macro. See README.txt in plone.z3cform for details. [optilude, davisagli]
Cleaned up templates to match Plone 4 conventions. [optilude]
Made templates and inline validation work with standalone forms as supported by plone.z3cform 0.6 and later. [optilude]
Installed a browser layer IPloneFormLayer with this package’s extension profile. This inherits from z3c.form’s IFormLayer, allowing the default widgets to work. You should always install this package in portal_quickinstaller before using z3c.form forms in Plone. [optilude]
Made the textlines widget the default for sequence types with text/ascii line value types. The default widget from z3c.form is too confusing. [optilude]
Use form method defined in form class. This allows HTTP GET forms. Before method was hardcoded to “post” in the template. [miohtama]
0.4.9 - 2010-01-08
Remove unused (and broken on Plone 4) lookup of the current user’s WYSIWYG editor preference. The wysiwyg_support template does this for us. [davisagli]
0.4.8 - 2009-10-23
Made the KSS validator use publish traversal instead of OFS traversal to find the form. This makes it usable with forms reached by custom IPublishTraverse adapters. [davisagli]
Added enable_form_tabbing option to not transform fieldsets into tabs. [vincentfretin]
Added an id to the generated form. [vincentfretin]
Fixed issue in macros.pt: fieldset.current hidden input was never generated. [vincentfretin]
0.4.7 - 2009-09-25
Set plone i18n domain for “Info” and “Error” messages in macros.pt so they are translated. [vincentfretin]
0.4.6 - 2009-07-26
Include plone.z3cform’s overrides.zcml from our own overrides.zcml. [optilude]
Updated to collective.z3cform.datetimewidget>=0.1a2 to fix a ZCML conflict with z3c.form. [davisagli]
0.4.5 - 2009-05-25
Made the KSS form support conditional on both kss.core and Archetypes being installed. [hannosch]
Use the date/time widgets from collective.z3cform.datetimewidget as the default widget for Date and Datetime fields. [davisagli]
0.4.4 - 2009-05-03
Made the KSS validator use traversal instead of getMultiAdapter() to find the form. This makes it work on add forms. See http://code.google.com/p/dexterity/issues/detail?id=27 [optilude]
0.4.3 - 2009-04-17
Added a display template for the WYSIWYG widget. [optilude]
Make the ?fieldset.current query string variable work. Set it to the id of a fieldset other than default to pre-select a different fieldset, e.g. …/@@formview?fieldset.current=3 [optilude]
Hide the ‘default’ fieldset if there’s nothing to show there. [optilude]
Provide ‘portal’ variable in wysiwyg template, as its used by some editors. [davisagli]
0.4.2 - 2008-09-04
Make the WYSIWYG widget work also for non-Acquisition wrapped content.
0.4.1 - 2008-08-21
Removed maximum version dependency on zope.component. This should be left to indexes, known good sets or explicit version requirements in buildouts. If you work with zope.component >= 3.5 you will also need five.lsm >= 0.4. [hannosch]
Make use of new plone.z3cform support for looking up the layout template by adapter. This means that forms now no longer need to depend on plone.app.z3cform unless they want to use Plone-specific widgets.
0.4.0 - 2008-07-31
Add inline validation support with KSS
Require zope.component <= 3.4.0 to prevent compatibility issues with five.localsitemanager, of which a buggy version (0.3) is pinned by plone.recipe.plone 3.1.4. Upgrade to this version if you’re seeing:
... Module five.localsitemanager.registry, line 176, in registeredUtilities ValueError: too many values to unpack
0.3.2 - 2008-07-25
Fixed a bug in macros.pt where ‘has_groups’ and ‘show_default_label’ for fieldsets were set in the ‘form’ macro, rendering the ‘field’ macro unusable by itself.
0.3.1 - 2008-07-24
Fixed a bug where we would use the form macros defined in plone.z3cform instead of our own.
0.3 - 2008-07-24
Create this package from Plone-specific bits that have been factored out of plone.z3cform.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.