I've been experimenting with the SelectField widget, and I found that
it does not render itself correctly when the option data is passed to
it in the render/insert call:

>>> from turbogears.widgets import *
>>> select = SelectField('test')
>>> select.render([(x,x) for x in range(10)])
'\n    <SELECT CLASS="select_field" NAME="test" ID="test">\n        \n
  </SELECT>'

I found the source of the problem in turbogears/widgets/base.py.  The
render/insert functions do not properly update the OptionWidget with
the input data.  I believe this happens because the OptionWidget uses
an "options" field to store its data, while the other widgets use
"widget_value".  I was able to fix the issue by changing the following
in base.py (starting at line 92):

    def render(self, value=None, input_values={}, error=None,
               format="html", convert=True, **kw):
        if not self.template:
            return None

+        if isinstance(self, OptionWidget) and value:
+            self.options = value

        return view.render(self.create_dict(value, input_values,
                                           error, convert=convert,
**kw),
                           template=self.template, fragment=1,
                           format=format)

    def insert(self, value=None, input_values={}, error=None,
               convert=True, **kw):
        if not self.template:
            return None

+        if isinstance(self, OptionWidget) and value:
+            self.options = value

        return view.transform(self.create_dict(value,
                                              input_values, error,
                                              convert=convert, **kw),
                              self.template)

(New results after the patch):
>>> from turbogears.widgets import *
>>> select = SelectField('test')
>>> select.render([(x,x) for x in range(10)])
'\n    <SELECT CLASS="select_field" NAME="test" ID="test">\n        \n
      \n
        <OPTION VALUE="0">0</OPTION>\n        \n        \n
<OPTION VALUE=
"1">1</OPTION>\n        \n        \n        <OPTION
VALUE="2">2</OPTION>\n
  \n        \n        <OPTION VALUE="3">3</OPTION>\n        \n
\n
<OPTION VALUE="4">4</OPTION>\n        \n        \n        <OPTION
VALUE="5">5</O
PTION>\n        \n        \n        <OPTION VALUE="6">6</OPTION>\n
  \n
    \n        <OPTION VALUE="7">7</OPTION>\n        \n        \n
<OPTION
VALUE="8">8</OPTION>\n        \n        \n        <OPTION
VALUE="9">9</OPTION>\n
        \n    </SELECT>'
>>>

This is the output I originally expected.  Am I doing something wrong,
or is this a bug?

Sean

Reply via email to