Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BootstrapRadioCheckboxDecorator prints input string #3

Open
peteristhegreat opened this issue Mar 7, 2021 · 0 comments
Open

BootstrapRadioCheckboxDecorator prints input string #3

peteristhegreat opened this issue Mar 7, 2021 · 0 comments

Comments

@peteristhegreat
Copy link

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.

@peteristhegreat peteristhegreat changed the title BootstrapRadioCheckboxDecorator prints input string BootstrapRadioCheckboxDecorator prints input string Mar 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant