Skip to main content

megrok extension for z3cform

Project description

megrok.z3cform.base is a not-so-thick layer above z3c.form. It provides a Grok way to register your forms and your widgets. In addition, the package has a collection of base forms, useable out-of-the box with grokcore.layout (or the old megrok.layout).

The customization of the forms is also eased by the use of megrok.pagetemplate, allowing you to override a template easily.

Form registration

Models

We set up some models to serve as a form context:

>>> import grokcore.component as grok
>>> from zope import interface, schema

>>> class IMammoth(interface.Interface):
...    name = schema.TextLine(title=u"Name")
...    age = schema.Int(title=u"Age")

>>> class Mammoth(grok.Context):
...    grok.implements(IMammoth)
...    name = schema.fieldproperty.FieldProperty(IMammoth['name'])
...    age = schema.fieldproperty.FieldProperty(IMammoth['age'])

We declare the Form. It’s very similar to a grok.View:

>>> import megrok.z3cform.base as z3cform

>>> class TestForm(z3cform.Form):
...    grok.context(Mammoth)

Grokking and querying

We let Grok register the component:

>>> grok.testing.grok_component('form', TestForm)
True

Now, we can query it normally:

>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> manfred = Mammoth()

>>> from zope.component import getMultiAdapter
>>> myform = getMultiAdapter((manfred, request), name="testform")

>>> myform
<TestForm object at ...>
>>> print myform()
<form action="http://127.0.0.1" method="post"
        enctype="multipart/form-data" class="form-testform">
...

Layout integration

megrok.z3cform.base is integrated, out-of-the-box with grokcore.layout, providing base classes to ease the layout integration in your project.

Let’s have a quick overview. We create a layout:

>>> import grokcore.layout

>>> class MyLayout(grokcore.layout.Layout):
...     grok.context(IMammoth)
...     def render(self):
...        return 'The layout content is: %s' % self.view.content()

We declare a Page Form. A Page Form is a form that will show up inside a layout:

>>> class PageForm(z3cform.PageForm):
...    grok.context(Mammoth)

We register the components with Grok:

>>> grok.testing.grok_component('page', PageForm)
True
>>> grok.testing.grok_component('layout', MyLayout)
True

Now, while rendering the form, we have it embedded in the Layout:

>>> pageform = getMultiAdapter((manfred, request), name="pageform")
>>> print pageform()
The layout content is: <form action="http://127.0.0.1" method="post"
      enctype="multipart/form-data" class="form-pageform">
...

Changelog

0.5 (2012-07-21)

  • Added support for the new grokcore.layout. No longer depends on megrok.layout directly. [danilobotelho]

0.4 (2012-06-22)

  • Added decorators for validator, invariant and default_value. The idea of these decorators is from plone.directives.form.
  • Added support for Groups (z3c.form.group) [danilobotelho]
  • Fixed WidgetTemplateGrokker [danilobotelho]
  • Added HIDDEN_MODE to __init__.py [danilobotelho]

0.3 (2010-02-11)

  • Fixed OS-dependent path handling in the widget template grokker.

0.2 (2009-11-18)

  • Corrected a bug where the ObjectModifiedEvent was trigged more than once during edition. [trollfot]
  • Upgrading to be compatible with grok 1.1. [trollfot]

0.1 (2009-10-20)

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for megrok.z3cform.base, version 0.5
Filename, size File type Python version Upload date Hashes
Filename, size megrok.z3cform.base-0.5.tar.gz (19.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page