Skip to content

Commit

Permalink
Append WTForms render_kw class for every field
Browse files Browse the repository at this point in the history
  • Loading branch information
VV-YY committed Apr 21, 2020
1 parent 3de2de6 commit 21fe081
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
------

- Fix ``enctype`` attribute setting for WTForms ``MultipleFileField``.
- Fix field class append bug when using ``render_kw={'class': 'my-class'}`` (`#53 <https://github.com/greyli/bootstrap-flask/issues/53>`__).

1.2.0
------
Expand Down
34 changes: 17 additions & 17 deletions flask_bootstrap/templates/bootstrap/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
{% call _hz_form_wrap(horizontal_columns, form_type, True, required=required) %}
<div class="form-group form-check{% if form_type == "inline" %} form-check-inline{% endif %}">
<label class="form-check-label">
{{ field(class_="form-check-input")|safe }} {{ field.label.text|safe }}
{{ field(class_="form-check-input %s" % field.render_kw.class)|safe }} {{ field.label.text|safe }}
</label>
</div>
{% endcall %}
Expand All @@ -54,12 +54,12 @@
this is just a hack for now, until I can think of something better #}
<div class="form-group {% if form_type == 'horizontal' %}row{% endif %}{% if required %} required{% endif %}">
{%- if form_type == "inline" %}
{{ field.label(class="sr-only")|safe }}
{{ field.label(class="sr-only %s" % field.render_kw.class)|safe }}
{% elif form_type == "horizontal" %}
{{ field.label(class="form-control-label " + (
" col-%s-%s" % horizontal_columns[0:2]))|safe }}
" col-%s-%s %s" % (horizontal_columns[0:2], field.render_kw.class)))|safe }}
{%- else -%}
{{ field.label(class="form-control-label")|safe }}
{{ field.label(class="form-control-label %s" % field.render_kw.class)|safe }}
{% endif %}
{% if form_type == 'horizontal' %}
<div class=" col-{{ horizontal_columns[0] }}-{{ horizontal_columns[2] }}">
Expand All @@ -83,7 +83,7 @@

{# note: same issue as above - should check widget, not field type #}
{% call _hz_form_wrap(horizontal_columns, form_type, True, required=required) %}
{{ field(class='btn btn-%s' % button_map.get(field.name, 'secondary'),
{{ field(class='btn btn-%s %s' % (button_map.get(field.name, 'secondary'), field.render_kw.class),
**field_kwargs) }}
{% endcall %}
{%- elif field.type in ['FormField', 'FieldList'] -%}
Expand All @@ -109,31 +109,31 @@
{{ field.label(class="sr-only")|safe }}
{% if field.type == 'FileField' %}
{% if field.errors %}
{{ field(class="form-control-file is-invalid", **kwargs)|safe }}
{{ field(class="form-control-file is-invalid %s" % field.render_kw.class, **kwargs)|safe }}
{% else %}
{{ field(class="form-control-file", **kwargs)|safe }}
{{ field(class="form-control-file %s" % field.render_kw.class, **kwargs)|safe }}
{% endif %}
{% else %}
{% if field.errors %}
{{ field(class="form-control mb-2 mr-sm-2 mb-sm-0 is-invalid", **kwargs)|safe }}
{{ field(class="form-control mb-2 mr-sm-2 mb-sm-0 is-invalid %s" % field.render_kw.class, **kwargs)|safe }}
{% else %}
{{ field(class="form-control mb-2 mr-sm-2 mb-sm-0", **kwargs)|safe }}
{{ field(class="form-control mb-2 mr-sm-2 mb-sm-0 %s" % field.render_kw.class, **kwargs)|safe }}
{% endif %}
{% endif %}
{% elif form_type == "horizontal" %}
{{ field.label(class="form-control-label " + (" col-%s-%s" % horizontal_columns[0:2]))|safe }}
<div class=" col-{{ horizontal_columns[0] }}-{{ horizontal_columns[2] }}">
{% if field.type == 'FileField' %}
{% if field.errors %}
{{ field(class="form-control-file is-invalid", **kwargs)|safe }}
{{ field(class="form-control-file is-invalid %s" % field.render_kw.class, **kwargs)|safe }}
{% else %}
{{ field(class="form-control-file", **kwargs)|safe }}
{{ field(class="form-control-file %s" % field.render_kw.class, **kwargs)|safe }}
{% endif %}
{% else %}
{% if field.errors %}
{{ field(class="form-control is-invalid", **kwargs)|safe }}
{{ field(class="form-control is-invalid %s" % field.render_kw.class, **kwargs)|safe }}
{% else %}
{{ field(class="form-control", **kwargs)|safe }}
{{ field(class="form-control %s" % field.render_kw.class, **kwargs)|safe }}
{% endif %}
{% endif %}
</div>
Expand All @@ -152,15 +152,15 @@
{{ field.label(class="form-control-label")|safe }}
{% if field.type == 'FileField' %}
{% if field.errors %}
{{ field(class="form-control-file is-invalid", **kwargs)|safe }}
{{ field(class="form-control-file is-invalid %s" % field.render_kw.class, **kwargs)|safe }}
{% else %}
{{ field(class="form-control-file", **kwargs)|safe }}
{{ field(class="form-control-file %s" % field.render_kw.class, **kwargs)|safe }}
{% endif %}
{% else %}
{% if field.errors %}
{{ field(class="form-control is-invalid", **kwargs)|safe }}
{{ field(class="form-control is-invalid %s" % field.render_kw.class, **kwargs)|safe }}
{% else %}
{{ field(class="form-control", **kwargs)|safe }}
{{ field(class="form-control %s" % field.render_kw.class, **kwargs)|safe }}
{% endif %}
{% endif %}
{%- if field.errors %}
Expand Down
27 changes: 23 additions & 4 deletions test_bootstrap_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def test():

response = self.client.get('/field')
data = response.get_data(as_text=True)
self.assertIn('<input class="form-control" id="username" name="username"', data)
self.assertIn('<input class="form-control" id="password" name="password"', data)
self.assertIn('<input class="form-control " id="username" name="username"', data)
self.assertIn('<input class="form-control " id="password" name="password"', data)

def test_render_form(self):
@self.app.route('/form')
Expand All @@ -112,8 +112,8 @@ def test():

response = self.client.get('/form')
data = response.get_data(as_text=True)
self.assertIn('<input class="form-control" id="username" name="username"', data)
self.assertIn('<input class="form-control" id="password" name="password"', data)
self.assertIn('<input class="form-control " id="username" name="username"', data)
self.assertIn('<input class="form-control " id="password" name="password"', data)

def test_render_form_row(self):
@self.app.route('/form')
Expand Down Expand Up @@ -360,3 +360,22 @@ def multi():
response = self.client.get('/multi')
data = response.get_data(as_text=True)
self.assertIn('multipart/form-data', data)


# test WTForm fields for render_form and render_field
def test_form_render_kw_class(self):
class TestForm(FlaskForm):
submit = SubmitField(render_kw={'class': 'my-awesome-class'})

@self.app.route('/render_kw')
def render_kw():
form = TestForm()
return render_template_string('''
{% from 'bootstrap/form.html' import render_form %}
{{ render_form(form) }}
''', form=form)

response = self.client.get('/render_kw')
data = response.get_data(as_text=True)
self.assertIn('my-awesome-class', data)
self.assertIn('btn', data)

0 comments on commit 21fe081

Please sign in to comment.