form's field validation / integer conversion

0 views
Skip to first unread message

remi jolin

unread,
Dec 8, 2006, 5:51:25 AM12/8/06
to turbo...@googlegroups.com
Hello,

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 ?

Alberto Valverde

unread,
Dec 9, 2006, 5:57:58 AM12/9/06
to turbo...@googlegroups.com

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

remi jolin

unread,
Dec 9, 2006, 1:26:34 PM12/9/06
to turbo...@googlegroups.com
Alberto Valverde a écrit :
Here is a small example.

----------------
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
>
> >
>

Alberto Valverde

unread,
Dec 10, 2006, 7:27:05 AM12/10/06
to turbo...@googlegroups.com

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

remi jolin

unread,
Dec 10, 2006, 9:57:33 AM12/10/06
to turbo...@googlegroups.com
Alberto Valverde a écrit :
>> (3, "cc")],
Thanks for pointing out my mistakes. It was my first test of form's
validation :-! It could not be a bug in TG.
Reply all
Reply to author
Forward
0 new messages