[Grok-dev] using megrok.form in your project

David Bain david.bain at alteroo.com
Mon Feb 25 13:00:34 EST 2008


Okay.. I've managed to get a megrok.form based project going with an
email field and an html field.

when I edit it says "constraint not satisfied"
 I didn't set  a constraint, so I'm not sure if there is some kind of
default constraint

below is the important code (I think)

app.py
-------------
import grok

class megrokformexample(grok.Application, grok.Container):
    pass

class Index(grok.View):
    pass # see app_templates/index.pt


demoform.py
---------------------
import grok
from megrok.form.fields import Email
from megrok.form.fields import HTML
from zope import interface
from z3c.widget.tiny.widget import TinyWidget

from app import megrokformexample

class IDemoForm(interface.Interface):

    email = Email(title=u'Email')
    document = Email(title=u'HTML')


class DemoForm(grok.Model):
    interface.implements(IDemoForm)

    def __init__(self, email, document):
        super(DemoForm, self).__init__()
        self.email = email
        self.document = document

class AddDemoForm(grok.AddForm):
    grok.context(megrokformexample)
    form_fields = grok.AutoFields(DemoForm)

    # Here is the trick. You set the 'custom_widget' attribute with
the custom Widget's class
    form_fields['document'].custom_widget = TinyWidget

    @grok.action('Add event')
    def add(self, **data):
        obj = DemoForm(**data)
        email= data['email'].lower().replace(' ', '_')
        document= unicode(data['document'])
        self.context[email] = obj
        self.context[document] = obj

class Edit(grok.EditForm):
    form_fields = grok.AutoFields(DemoForm)


class Index(grok.DisplayForm):
    pass

and an addform based on the default form that ships with grok


More information about the Grok-dev mailing list