TGExpandingFormWidget is a type of repeating form widget for TurboGears. It contains groups of widgets which are repeated on the page. The user is able to dynamically add or remove widget groups as required before submitting the form.
Install with setuptools:
$ easy_install TGExpandingFormWidget
Or download:
from turbogears import controllers, expose, flash, validators, widgets, validate, error_handler, redirect
from tg_expanding_form_widget.tg_expanding_form_widget import ExpandingForm
# Validation Schemas
class FormSchema(validators.Schema):
firstname = validators.UnicodeString(not_empty=True, max=256, strip=True)
lastname = validators.UnicodeString(not_empty=True, max=256, strip=True)
class ExpandingFormSchema(validators.Schema):
people = validators.ForEach(
FormSchema(),
)
# Form Widgets
firstname = widgets.TextField(name='firstname', label=_(u'First Name'), attrs=dict(size=30))
lastname = widgets.TextField(name='lastname', label=_(u'Last Name'), attrs=dict(size=30))
expform = ExpandingForm(
name='people',
label=_(u'People I like'),
fields=[firstname, lastname],
)
expanding_form = widgets.ListForm(
'peopleform',
fields = [expform],
action = 'save_data',
submit_text = _(u'Submit Data'),
validator = ExpandingFormSchema()
)
class Root(controllers.RootController):
@expose(template="tgexpandtest.templates.welcome")
def index(self):
import time
flash("Your application is now running")
return dict(now=time.ctime())
@expose(template='.templates.edit_people')
def edit_people(self):
values = dict(
people = [
dict(firstname='Homer', lastname='Simpson'),
dict(firstname='Peter', lastname='Griffin'),
]
)
return dict(
form = expanding_form,
action = 'save',
options = {},
value = values,
)
@expose()
@validate(form=expanding_form)
@error_handler(edit_people)
def save(self, people=None):
print "people: %s" %people # output to logfile
for person in people:
print "Person: %s %s" %(person['firstname'], person['lastname'])
flash( _(u"OK") )
redirect(".")
and edit_people.kid should contain:
<p py:content="form(method='GET', action=action, value=value, options=options)">Form goes here</p>
© Chris Miles 2007