Zope 3 add forms

Posted 17 Jul 2006 at 09:11 UTC by MJ Share This

Marius Gedminas shows us how to create a simple add form for Zope3 using formlib, but he then assumes the object in question accepts keywords in the __init__ method. Here's how you can use an additional formlib trick if you don't have such a handy __init__ method:

# This is the mypackage.browser module
from zope.formlib import form
 
from mypackage.interfaces import IFruit
from mypackage.fruit import Fruit
 
class FruitAdd(form.AddForm):
 
    form_fields = form.Fields(IFruit)
 
    def create(self, data):
         # applyChanges applies all changed fields named in form_fields
         # to the object, taking the field values from data.
         # In a new object, that's *all* fields.
         fruit = Fruit()
         form.applyChanges(fruit, self.form_fields, data)
         return fruit

applyChanges is also very handy if you use multiple interfaces to build your form_fields variable, e.g. adding IZopeDublinCore fields into the mix. This handy utility method will adapt your object to the correct interface for each and every field to accomplish it's task!

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!

X
Share this page