Forms with values in TurboGears

So 0.9a1 supports forms, that’s great. However, I found getting hold of suitable examples for use in the real world difficult. The examples all show how to create a form and display it, and also how to validate the information in the response. What they didn’t show, is how to get information into the form when it’s displayed.

So here is a quick sample showing how it’s done:

In your python code, you build up a dictionary of the field values:

<code>class SomeFields(widgets.WidgetsDeclaration):
    name = widgets.TextField(validator=validators.NotEmpty)
    email = widgets.TextField(validator=validators.Email())

the_form = widgets.TableForm(fields=SomeFields(), submit_text="Post")

class Root(RootController):
    @expose(template=".templates.index")
    def index(self):
        values = {"name":"simon", "email":"me@there.com"}
        return dict(form=the_form, values=values)
</code>

Then in your kid template, you pass the values in to the form:

${form(action="submitForm", value=values)}