Sat, 19 May 2007

passing options to toscawidgets

The syntax for passing options to a widget with toscawidgets is slightly different than it was with the turbogears widgets. This actually had me google searching for a while to find this so I thought I'd post it here as a full example:

You define your widget in the controllers.py file:
from toscawidgets.core import WidgetsList

from toscawidgets.widgets.forms import SingleSelectField, TableForm, TextField
from formencode.schema import Schema
...
class FilteringSchema(Schema):
filter_extra_fields = True
allow_extra_fields = True

class Fields(WidgetsList):
testselect = SingleSelectField(label="Test:", validator=validators.Int())
name = TextField(validator=validators.NotEmpty(), label="Name:", attrs={'maxlength':128})

testform = TableForm("TestForm", validator=FilteringSchema, fields=Fields(), action="save", submit_text="Submit")

class Root(controllers.RootController):

@expose(html="test.templates.formpage")
def formpage(self):
selectlist = [(1, 'option 1'), (2, 'option 2')]
data = dict(child_args=dict(testselect=dict(options=selectlist)), value=dict(name='Test Guy'))
return dict(form=testform, data=data)


This produces your form and the data to populate your form passed as form and data. In the formpage template you simply display the form with the following line:
${form.display(**data)}


This should produce a form with a select field showing option 1 and option 2 in a dropdown, and a name text field filled in with the value Test Guy.

posted at: 01:45 | path: /turbogears | permanent link to this entry



Powered by PyBlosxom | RSS 2.0