Am 13.06.2010 um 17:25 schrieb Michel Albert:
Hi,
I've a small problem with TG2 and formencode. I have a set of three
checkboxes. These three are fairly static (i.e. no items will be added
to that list). So I don't see the point of creating a form-widget, and
rather hard-code it into HTML. But how should I name the form elements
so that formencode handles them properly? Additionally: which
validator should I use for a checkbox list? validators.Set? Or is
there something else?
I've seen in the docs, that fields which can take multiple values
should be named "item-1", "item-2", ... , "item-n". I tried this like
the following, but it didn't work:
<input type="checkbox" name="foo-1" value="23" />
<input type="checkbox" name="foo-2" value="42" />
<input type="checkbox" name="foo-3" value="64" />
I'd like this to feed the following controller-method:
@validate( validators={ "foo": validators.Set() } )
def my_foos( self, foo=None ):
pass
Or something along those lines.
You could have answered that question yourself very easy by creating a
little TW form that just does that.
>>> from tw.forms import *
>>> f = TableForm('test', fields=[CheckBoxList("foo", options=["a",
"b", "c"])])
>>> f.render()
u'<form xmlns="http://www.w3.org/1999/xhtml" id="test" action=""
method="post" class="tableform">\n <table border="0"
cellspacing="0" cellpadding="2">\n <tr id="test_foo.container"
class="even" title="">\n <td class="labelcol">
\n <label id="test_foo.label" for="test_foo"
class="fieldlabel">Foo</label>\n </td>\n <td
class="fieldcol">\n <ul class="checkboxlist"
id="test_foo">\n <li>\n <input type="checkbox" name="foo"
id="test_foo_0" value="a" />\n <label for="test_foo_0">a</label>
\n </li><li>\n <input type="checkbox" name="foo"
id="test_foo_1" value="b" />\n <label for="test_foo_1">b</label>
\n </li><li>\n <input type="checkbox" name="foo"
id="test_foo_2" value="c" />\n <label for="test_foo_2">c</label>
\n </li>\n</ul>\n </td>\n </tr><tr
id="test_submit.container" class="odd" title="">\n <td
class="labelcol">\n </td>\n <td class="fieldcol">
\n <input type="submit" class="submitbutton"
id="test_submit" value="Submit" />\n </td>\n </tr>
\n </table>\n</form>'
>>>
So as you see, it's fine to simply use the single name. As validator,
I suggest the ForEach-validator with always_list (or something
similar) set to True, and the passed validator to ForEach of course
being OneOf.
I'm not 100% sure, but I guess the -1,-2, ... stuff only applies to
compound fields, because HTTP doesn't hav mechanism for these.
BTW, I think it would be worth using tw.forms even for these small
cases.
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.