I was wanting to have horizontal radio buttons in my forms, so I set
about making my own widget. The widget works well, except for one odd
behavior. When I load the form, the first radio button is selected as
desired. But after the form submits, page refreshes etc, the last
radio buttons in the list are selected. IE, rather than defaulting to
the first element selected as it did the first time, it defaults to
the last. As this form will be submitted many times in a row, and the
first option is the most often used, this proves to be a bit of a
pain. I've tried this in multiple browsers to the same effect, so I'm
pretty sure it's happening due to how the code is written. But I'm not
really sure where this behavior is written. Anyone have any ideas?

The code for my modified widget is below:

class RadioWidget_Horizontal(OptionsWidget):

    @staticmethod
    def widget(field, value, **attributes):
        """
        generates an unordered list of radio buttons, which has
        a css class radio_list so it can be styled inline.

        see also: :meth:`FormWidget.widget`
        """

        attr = OptionsWidget._attributes(field, {}, **attributes)
        attr['_class'] = attr['_class'] + " radio_list"

        if hasattr(field.requires, 'options'):
            options = field.requires.options()
        else:
            raise SyntaxError, 'widget cannot determine options of %s'
% field
        opts = [LI(INPUT(_type='radio', _name=field.name,
                         requires=attr.get('requires',None),
                         hideerror=True, _value=k,
                         value=value), v) for (k, v) in options if
str(v)]
        if opts:
            opts[-1][0]['hideerror'] = False
            opts[0][0]['_checked'] = "checked"
        return UL(*opts, **attr)

Reply via email to