- Evan
- Evan
As I've just said in a reply to Joseph's post labels cannot yet be
overriden on update_params since it's the form that renders it, not the
field. However, you might want to try to make a TextField (or whatever)
subclass that displays it's own labels and tell the form not to display
them (I think Paul applied a patch to do just that, take a look at the
Form's params list see if you find anything.
Alberto
- Evan
Yes, it is called for every widget
>Could I somehow
> initialize the children of a form in update_params? I think I tried a
> bunch of things and couldn't make this work...
No, all children/fields must be known at widget initialization time since
they need to be grafted into a tree structure for correct "name" attr.
generation (for input widgets) and "id" attr generation (for all widgets),
a validator created for input widgets and many other things.
I think that the Widget's docstring explains something about it.
Probably the best you can do in this situation is to mimic the label with
a widget you can send a value to with the text, put it in a fieldset or
some container and then use a widget repeater to repeat each container as
a row.
Alberto
Ok, so I've spent a couple hours fighting with this, and I could use
some more advice. Specifically, it's not just good enough for me to be
able to control the labels and lengths of a list of input fields; I also
need some way to map each input field back to a row in my database.
To outline the basic application, I'm asking for a percentage for each user.
Here's the form declaration I have right now:
> from tw import forms
> from tw.api import WidgetsList
>
> from sample import model
> from sample.model import meta
>
> class PercentageField(forms.TextField):
> template = 'percentage_field'
> suppress_label = True
> label_text = ''
>
> class PercentageFieldSet(forms.ListFieldSet):
> suppress_label = True
>
> class fields(WidgetsList):
> percentage = forms.FormFieldRepeater(
> widget=PercentageField())
>
> def update_params(self, d):
> users = meta.Session.query(model.User)
>
> if 'child_args' not in d:
> d['child_args'] = dict()
>
> d['child_args']['percentage'] = dict(repetitions=users.count(),
> child_args=[])
>
> for i, user in enumerate(users):
> d['child_args']['split']['child_args'].append(
> dict(label_text=user.name))
>
> return super(PercentageFieldSet, self).update_params(d)
The percentage_field template is basically the label code from the
normal ListFieldSet plus a TextField (I'm using mako):
> <%namespace name="tw" module="tw.core.mako_util"/>\
> <label ${tw.attrs(
> [('id', '%s.label' % id),
> ('for', id),
> ('class', 'fieldlabel' + (' required' if is_required else ''))]
> )}>${tw.content(label_text)}</label>
> <input ${tw.attrs(
> [('type', context.get('type')),
> ('name', name),
> ('class', css_class),
> ('id', context.get('id')),
> ('value', value)],
> attrs=attrs
> )} />\
And this is very close to what I want. It currently outputs something like:
> <fieldset id="percentage" class="percentagefieldset">
> <legend>percentage</legend>
> <ul class="field_list" >
> <li class="even" id="percentage.container"
> >
> <label id="percentage_percentage-0.label"
> for="percentage_percentage-0" class="fieldlabel">Evan Broder</label>
> <input type="text" name="percentage.percentage-0"
> class="repeatedpercentagefield" id="percentage_percentage-0" value="" />
> <label id="percentage_percentage-1.label"
> for="percentage_percentage-1" class="fieldlabel">User 2</label>
> <input type="text" name="percentage.percentage-1"
> class="repeatedpercentagefield" id="percentage_percentage-1" value="" />
> <label id="percentage_percentage-2.label"
> for="percentage_percentage-2" class="fieldlabel">User 3</label>
> <input type="text" name="percentage.percentage-2"
> class="repeatedpercentagefield" id="percentage_percentage-2" value="" />
> <label id="percentage_percentage-3.label"
> for="percentage_percentage-3" class="fieldlabel">User 4</label>
> <input type="text" name="percentage.percentage-3"
> class="repeatedpercentagefield" id="percentage_percentage-3" value="" />
> </li>
> </ul>
> </fieldset>
But I have no way to connect percentage.percentage-3 back to User 4 in
my database. I'd like it to instead generate ids of the form
"percentage.percentage.3" instead, where the 3 is the ID field in the
database.
I realize that this question probably isn't coming off too well, since
I've had no luck attempting to grok ToscaWidgets' internals, but is
there any way to do this?
- Evan