Skip to content

BootstrapRadioCheckboxDecorator prints input string #3

@peteristhegreat

Description

@peteristhegreat

As is, it escapes the input field and writes it as part of the label string.

This snippet fixes it for me:
wtforms_widgets/widgets.py

class BootstrapRadioCheckboxDecorator(WidgetDecorator):
    """
    Renders a field in horizontal layout.

    Horizontal layout is a two column layout, where the label is placed in the
    left column and the field is placed right next to it.
    """
    wrapper_class = None

    def _render(self, field, **kwargs):
        return HTMLString(u''.join([
            u'<div class="', self.wrapper_class, u'">',
            u'<label class="', self.wrapper_class + "-label", u'">',
            self.widget(field, class_=self.wrapper_class + "-input", **kwargs),
            escape(field.label.text),
            u'</label>',
            u'</div>',
        ]))

    def render_basic(self, field, **kwargs):
        return self._render(field, **kwargs)

    def render_horizontal(self, field, **kwargs):
        return HTMLString(u''.join([
            u'<div class="offset-sm-4 col-sm-4">',
            self._render(field, **kwargs),
            u'</div>',
        ]))

    def render_inline(self, field, **kwargs):
        return HTMLString(u''.join([
            u'<div class="', self.wrapper_class + "-inline", u'">',
            u'<label class="', self.wrapper_class + "-label", u'">',
            self.widget(field, class_=self.wrapper_class + "-input", **kwargs),
            escape(field.label.text),
            u'</label>',
            u'</div>',
        ]))

    def __call__(self, field, **kwargs):
        render_mode = kwargs.pop("render_mode", "basic")
        if render_mode == "basic":
            return self.render_basic(field, **kwargs)
        elif render_mode == "horizontal":
            return self.render_horizontal(field, **kwargs)
        elif render_mode == "inline":
            return self.render_inline(field, **kwargs)
        else:
            raise ValueError("Unknown render mode: {0}".format(render_mode))


class BootstrapRadioDecorator(BootstrapRadioCheckboxDecorator):
    wrapper_class = u"form-check"


class BootstrapCheckboxDecorator(BootstrapRadioCheckboxDecorator):
    wrapper_class = u"form-check"

I'll try to make a PR in the near future. Thanks for the awesome library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions