Wednesday, September 26, 2007

Toscawidgets Forms: Passing Compound Widgets

For lack of documentation's sake, and because it wasn't initially obvious to me after poking around in the code (in fact, I got led astray, thinking there was some dot notation I would have to use), this is how you pass values into Compound Widgets in Toscawidgets:

Say you have some Widget like this:
form = Form('FormName',
children=[
FieldSet('FieldSetName',
children=[
TextField('TextFieldName')])
])

and displayed in your template like this:

${ form.display(**form_args)}


then you can pass values into it from your controller like so:
def controller(self):
return dict(form=form,
form_args={'value':{
'FieldSetName':{ 'TextFieldName':
'Desired TextField Value'}}})

I guess it's pretty obvious in retrospect, in fact, the compound Form outputs something much like this (minus the outside dictionary), and those values can be passed right back into the Compound Form again. Perhaps, had I been actually using a Model, or gotten more sleep, this all would have been more natural - but hopefully this helps someone else.

Hint: If you're overriding templates, be sure that you're still passing the right values in to fetch the children arguments, i.e.:


${section.display(value_for(section), **args_for(section))}

No comments: