> 2. Another use of this is that I have what is the equivalent to a
> custom search form. However, the controls/fields on this form will
> vary depending on where they are in the site. So, as an example where
> this specifically happens is in generating a series of checkbox lists
> grouped by "categories" where each category is its own checkboxlist
> widget with its own options.
The below works for me - albeit I admit it is not really obvious.
And I haven't tested this with validation:
--------------------
import tw.api as tw
import tw.forms as twf
# takes a fields-parameter with one widget as element.
class ToggleWidget(twf.ContainerMixin, twf.FormField):
params = dict(enabled="Is the child widget enabled")
enabled = True
engine_name = "genshi"
template = """<div py:strip="True"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/" py:if="enabled"
>${display_child(fields[0])}</div>"""
form = twf.ListForm("form", fields=[
ToggleWidget("names", fields=
[twf.CheckBoxList("name", options=["Peter", "Paul",
"Mary"])]
),
ToggleWidget("pets", fields=
[twf.CheckBoxList("name", options=["Lassie", "Flipper"])]
),
])
print form.display(child_args=dict(names=dict(enabled=True,
child_args=dict(name=dict(options=["Foo", "Bar"]))),
pets=dict(enabled=False)
)
)
---------------------
A similar approach might work with your use-case #1
> So, I figure the solution here would be just to create a new form
> widget per request that initializes the fields it needs. Is there
> another route? It just seems like creating variable field forms
> shouldn't be so difficult; unless I'm not understanding something here
> and just need to arrive at the, doh! moment! with TW.
It will cause a memory-leak. This is because to fix a bug I had to
create a global registry that will hold a reference to every widget that
is created.
Instead, I would recommend that you create a widget that simply renders
the whole form. And that is parametrized by the values you need.
I will try and dig deeper into tw.forms to see if there isn't a better
option, but currently the best I can offer is the above widget.
Diez
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---