Hi all,

I've been reading the widgets/base.py code and just don't see how you
tell a CheckBoxList which items are already checked. Anyone have a
working example, or can say quickly how to do it?

Also, here's the bug(s):

In short, the widget's values coming from a submitted form are not
consistent. You feed it a list but it doesn't always return one.

If you check nothing, it should return an empty list. (no value at all
is supplied to the function). Presumably this is because the browser
will return nothing, but the widget knows that list was there and
should correct for this condition.

If you only check one item, it returns the value of that one item as a
string (should be a one-item list with the value).

If you check more than one item, you get what you'd expect (a list of
the values).

The inconsistency will mean extra coding work to process the incoming
data every time you use the CheckBoxList (does the value exist? is it a
string or a list of strings? then process accordingly.).

For testing, I put together a simple set of Articles and Topics:

the model:
=========

class Topic(SQLObject):
    name=StringCol()
    articles=RelatedJoin('Article')

class Article(SQLObject):
    title=StringCol()
    body=StringCol()
    topics=RelatedJoin('Topic')


the form (widgets imported as W)
=======================

articleForm = W.TableForm(
    widgets=[
        W.Hidden(name='id'),
        W.TextField(name='title'),
        W.TextArea(name='body'),
        W.CheckBoxList(name='topics',options=[(t.id,t.name) for t in
Topic.select()]),
    ])

relevant bits from controllers.py
=================

    @turbogears.expose(template='testing.templates.articleform')
    def articleedit(self,id):
        return dict(theForm=articleForm, article=Article.get(int(id)))

    @turbogears.expose(inputform=articleForm)
    def articlesave(self,id,title,body,topics=[]):
        a = Article.get(id)
        a.title=title
        a.body=body
        turbogears.flash("topics were: %s, type was: %s" %
(repr(topics), type(topics)))
        raise cherrypy.HTTPRedirect(turbogears.url('/articleedit/%s' %
id))

Reply via email to