Skip to content

Commit

Permalink
Fix csrf token label of render field macro (#131)
Browse files Browse the repository at this point in the history
* Fix csrf token label of render field macro

* Add support for HiddenField
  • Loading branch information
yuxiaoy1 authored May 18, 2021
1 parent 0fc18ce commit a6f8256
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions flask_bootstrap/templates/bootstrap/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
{% set default_button_size = button_size or config.BOOTSTRAP_BTN_SIZE %}
{{ field(class='btn btn-%s btn-%s%s' % (button_map.get(field.name, default_button_style), default_button_size, extra_classes), **field_kwargs) }}
{% endcall %}
{%- elif field.type in ['CSRFTokenField', 'HiddenField'] -%}
{{ field()|safe }}
{%- elif field.type in ['FormField', 'FieldList'] -%}
{# note: FormFields are tricky to get right and complex setups requiring
these are probably beyond the scope of what this macro tries to do.
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
from flask import Flask, render_template_string
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import BooleanField, PasswordField, StringField, SubmitField
from wtforms import BooleanField, PasswordField, StringField, SubmitField, HiddenField
from wtforms.validators import DataRequired, Length


class HelloForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(1, 20)])
password = PasswordField('Password', validators=[DataRequired(), Length(8, 150)])
remember = BooleanField('Remember me')
hidden = HiddenField()
submit = SubmitField()


Expand Down
4 changes: 4 additions & 0 deletions tests/test_render_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ def test():
form = hello_form()
return render_template_string('''
{% from 'bootstrap/form.html' import render_field %}
{{ render_field(form.csrf_token) }}
{{ render_field(form.hidden) }}
{{ render_field(form.username) }}
{{ render_field(form.password) }}
''', form=form)

response = client.get('/field')
data = response.get_data(as_text=True)
assert '<label class="form-control-label" for="csrf_token">CSRF Token</label>' not in data
assert '<label class="form-control-label" for="hidden">Hidden</label>' not in data
assert '<input class="form-control" id="username" name="username"' in data
assert '<input class="form-control" id="password" name="password"' in data

0 comments on commit a6f8256

Please sign in to comment.