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

BooleanField rendering with extra classes has the class keyboard repeated #155

Closed
coreybrett opened this issue Sep 10, 2021 · 2 comments · Fixed by #156
Closed

BooleanField rendering with extra classes has the class keyboard repeated #155

coreybrett opened this issue Sep 10, 2021 · 2 comments · Fixed by #156

Comments

@coreybrett
Copy link

I have this...

safe_ha01 = BooleanField("HA - Access / Egress", render_kw={"class": "xyz"})

and it is rendering as...

<div class="form-group form-check"><input class="xyz" class="form-check-input xyz" id="safe_ha01" name="safe_ha01" type="checkbox" value="y">
                <label class="form-check-label" for="safe_ha01">HA - Access / Egress</label>
            </div>

Am I doing something wrong?

@yuxiaoy1
Copy link
Contributor

Good catch.
I think you're doing it the right way, but there's a problem with how Bootstrap-Flask handleing BooleanField, so nothing you can do currently to get the correct result.
To simply fix this, the class_ below need to be renamed to class.
https://github.com/greyli/bootstrap-flask/blob/a1ded400dbc91332a4e23fbc29ab791d92169325/flask_bootstrap/templates/bootstrap/form.html#L65-L67
I will make a PR fixing this issue.

PS. A fun fact is that the latter class class="form-check-input xyz" would be omitted by browser, so you can only get class="xyz" in the browser.

<div class="form-group form-check">
    <input class="xyz" id="safe_ha01" name="safe_ha01" type="checkbox" value="y">
    <label class="form-check-label" for="safe_ha01">HA - Access / Egress</label>
</div>

This is specified in the HTML standard, with reference to https://html.spec.whatwg.org/multipage/parsing.html#attribute-name-state

@yuxiaoy1
Copy link
Contributor

I just made a PR fixing this.
But I want to discuss more on this issue, actually, the root cause is how wtforms handles class_ and class, because of the
frequent use of the normally reserved keywords class( and for ), suffixing these with an underscore will allow them to be used. But when they were used at the same time, it would result in two class attributes in the markup, which doesn't make sense.
I'm considering making another PR to wtforms to improve this.
So class_ is definitely not a typo here, but we need to keep consistency when setting class with the current version of wtforms.

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

Successfully merging a pull request may close this issue.

2 participants