On Jul 7, 11:03 pm, iain duncan <[EMAIL PROTECTED]> wrote:
> I would recommend very strongly against that, myself. The widgets are
> difficult to learn, and I've pulled many hairs out over them, but once
> you get it they are fantastically powerful.
>
> For the above, you actually have a few options. I'd recommend you make
> your own widget class that subclasses list_form, and then you can
> either:
>
> - change the template to do what you want
> - override update_params in the widget to change the field.label_text
> for the field in question
>
> Or you could make a new field widget that subclasses your field and
> alter it there. I'm doing both in my case. It seems like a bit of a
> headache at first, but making your own derived widgets opens up a lot of
> handy options.
>
> There are some good examples coming up on the TosaWidgets site now too.
>
> my two cents!
> Iain
Thanks for all your help, this is what I've come up with..
class AltListForm(widgets.ListForm):
template = """
<form xmlns:py="http://purl.org/kid/ns#"
name="${name}"
action="${action}"
method="${method}"
class="listform"
py:attrs="form_attrs"
>
<div py:for="field in hidden_fields"
py:replace="field.display(value_for(field),
**params_for(field))"
/>
<ul py:attrs="list_attrs">
<li py:for="i, field in enumerate(fields)"
class="${field.field_class} ${i%2 and 'odd' or
'even'}"
>
<label py:if="field.field_class != 'checkbox'"
class="fieldlabel" for="${field.field_id}"
py:content="labels.get(field.field_id, field.label)" />
<span py:replace="field.display(value_for(field),
**params_for(field))" />
<label py:if="field.field_class == 'checkbox'"
class="fieldlabel checkboxlabel" for="${field.field_id}"
py:content="labels.get(field.field_id, field.label)" />
<span py:if="error_for(field)" class="fielderror"
py:content="error_for(field)" />
<span py:if="field.help_text" class="fieldhelp"
py:content="field.help_text" />
</li>
<li py:content="submit.display(submit_text)" />
</ul>
</form>
"""
params = ["labels"]
params_doc = {'labels': 'A dict allowing modification of labels'}
labels = {}
And in my template:
${ET(my_form.display(data, labels=labels))}
In the controller I can do something like:
res['labels'] = {}
if some_condition:
res['labels'].update({'form_myfield': 'My altered label',})
This is not ideal, as I need to include the 'form_' prefix to match
the particular path that the field has in the form - a gotcha (is
there a way to access the fields' id as it was declared?).
On another topic - you may notice a few other modifications to the
template above - namely the reordering of CheckBox label elements so
that they appear after their input element. My initial expectation
was that the rendering of the label, errors and help text should be
delegated to the field widget, rather than being rendered in the form
- I assume this is in order to accomodate TableForms.
Eoghan
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---