I have a form with a MultipleSelectField(validator=validators.Int()),
options are something like [(1, "aaa"), (2, "bbb"), ...]
If I select a single element I get an integer. That's OK.
But, if I select multiple elements, I get a list of strings like [u'1',
u'2'] where I was hopping to get [1, 2].
Is it the standard behaviour ?
No. You should be geting a list of ints. It might be a bug... :( Can
you send some code that reproduces this problem (a test case that
could be included in the widgets' test suite would be awesome) so I
can check it out?
Thanks :)
Alberto
----------------
import logging
import cherrypy
import turbogears
from turbogears import controllers, expose, validate, redirect
from turbogears import widgets, validators
log = logging.getLogger("mm.controllers")
class MyField(widgets.WidgetsList):
my_selection = widgets.MultipleSelectField(options=[(1, "aa"),
(2, "bb"),
(3, "cc")])#,
#validator=validators.Int())
class MultipleSelectForm(widgets.TableForm):
fields = MyField()
class Root(controllers.RootController):
@expose(template="mm.templates.test_select")
def test_select(self, my_selection=None, **kw):
print ">>> ", my_selection, " <<<"
return dict(MultipleSelectForm=MultipleSelectForm())
@expose(template="mm.templates.welcome")
def index(self):
import time
log.debug("Happy TurboGears Controller Responding For Duty")
return dict(now=time.ctime())
----------------------------
test_select.kid has a single <div>${MultipleSelectForm.display()}</div>
When I call test_select and select a single element (bbb) the print
gives >>> 2 <<<
If I select "aa" and "cc" I get >>> [u'1', u'3'] <<<
Another question is why I don't get a list (like [2]) when I select a
single element ??
I tried to setup a test like this
form = MultipleSelectForm()
v = dict(MySelection=[u'1', u'2']
r = form.validate(v)
I get [1, 2] in r has expected...
(TurboGears 1.0b1)
> Alberto
>
> >
>
Good! That means nothing is broken :) The form is validating correctly
when told explicitly to do so, but that's not happening in "test_select".
To do so you need to decorate test_select with:
@validate(form=my_form)
so it validates and converts input. This also means you have to
instantiate my_form outside the controller methdod so @validate has access
to it. You can do that at the module level or inside your controller
class.
Alberto