From bc5c62e472a942c904bb89d50948ec108287b464 Mon Sep 17 00:00:00 2001 From: Pander Date: Sun, 31 Dec 2023 01:54:02 +0100 Subject: [PATCH 1/2] rendering labels and description with classes --- docs/advanced.rst | 28 ++++++ docs/macros.rst | 2 +- examples/bootstrap5/app.py | 36 +++++-- examples/bootstrap5/templates/form.html | 20 ++-- .../templates/bootstrap5/form.html | 54 ++++++++--- tests/conftest.py | 22 ++++- tests/test_bootstrap5/test_render_form.py | 94 +++++++++++++++++++ 7 files changed, 227 insertions(+), 29 deletions(-) diff --git a/docs/advanced.rst b/docs/advanced.rst index 88e3023b..eb324437 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -140,6 +140,34 @@ Bootstrap-Flask, simply use the built-in class ``SwitchField()`` instead of ``BooleanField()``. See also the example application. +.. _inputcustomization: + +Form Input Customization +------------------------ + +Rendering Label +~~~~~~~~~~~~~~~ + +Bootstrap offers control for rendering +`text `_. This is supported +for inputs of a form by adding ``render_kw={'label_class': '... ...'}`` to the +field constructor. In order to control the rendering of the label of a field, +use ``render_kw={'label_class': '... ...'}``. See also the example application. + +Rendering Radio Label +~~~~~~~~~~~~~~~~~~~~~ + +Similar support exists for the rendering of the labels of options of a +``RadioField()` with ``render_kw={'radio_class': '... ...'}``. See also the +example application. + +Rendering Description +~~~~~~~~~~~~~~~~~~~~~ + +Use ``render_kw={'descr_class': '... ...'}`` for controlling the rendering of a +field's description. See also the example application. + + .. _bootswatch_theme: Bootswatch Themes diff --git a/docs/macros.rst b/docs/macros.rst index 63f13dbd..78ba9a4d 100644 --- a/docs/macros.rst +++ b/docs/macros.rst @@ -131,7 +131,7 @@ API form group classes, it will read the config ``BOOTSTRAP_FORM_GROUP_CLASSES`` first (the default value is ``mb-3``). -.. tip:: See :ref:`button_customization` and :ref:`checkbox_customization` to learn more on customizations. +.. tip:: See :ref:`button_customization`, :ref:`checkbox_customization` and :ref:`input_customization` to learn more on customizations. render_form() diff --git a/examples/bootstrap5/app.py b/examples/bootstrap5/app.py index acb85b23..f816b994 100644 --- a/examples/bootstrap5/app.py +++ b/examples/bootstrap5/app.py @@ -31,10 +31,10 @@ class ExampleForm(FlaskForm): """An example form that contains all the supported bootstrap style form fields.""" - date = DateField(description="We'll never share your email with anyone else.") # add help text with `description` - datetime = DateTimeField(render_kw={'placeholder': 'this is a placeholder'}) # add HTML attribute with `render_kw` + date = DateField(description='Your memorable date') # add help text with `description` + datetime = DateTimeField(render_kw={'placeholder': 'this is a placeholder', 'class': 'text-decoration-line-through'}) # add HTML attribute with `render_kw` datetime_local = DateTimeLocalField() - time = TimeField() + time = TimeField(description='This is private', render_kw={'descr_class': 'fst-italic text-decoration-underline'}) month = MonthField() color = ColorField() floating = FloatField() @@ -45,27 +45,43 @@ class ExampleForm(FlaskForm): url = URLField() telephone = TelField() image = FileField(render_kw={'class': 'my-class'}, validators=[Regexp('.+\.jpg$')]) # add your class - option = RadioField(choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')]) + option = RadioField(choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')], render_kw={'label_class': 'text-decoration-underline', 'radio_class': 'text-decoration-line-through'}) select = SelectField(choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')]) select_multiple = SelectMultipleField(choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')]) bio = TextAreaField() - search = SearchField() # will autocapitalize on mobile - title = StringField() # will not autocapitalize on mobile + search = SearchField() # will autocapitalize on mobile + title = StringField() # will not autocapitalize on mobile secret = PasswordField() remember = BooleanField('Remember me') submit = SubmitField() +class ExampleFormInline(FlaskForm): + """An example inline form.""" + floating = FloatField(description='a float', render_kw={'label_class': 'text-decoration-underline'}) + integer = IntegerField(description='an int', render_kw={'descr_class': 'text-decoration-line-through'}) + option = RadioField(description='Choose one', choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')], render_kw={'radio_class': 'text-decoration-line-through', 'descr_class': 'fw-bold'}) + submit = SubmitField() + + +class ExampleFormHorizontal(FlaskForm): + """An example horizontal form.""" + floating = FloatField(description='a float', render_kw={'label_class': 'text-decoration-underline'}) + integer = IntegerField(description='an int', render_kw={'descr_class': 'text-decoration-line-through'}) + option = RadioField(description='choose 1', choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')], render_kw={'label_class': 'text-decoration-underline'}) + submit = SubmitField() + + class HelloForm(FlaskForm): username = StringField('Username', validators=[DataRequired(), Length(1, 20)]) password = PasswordField('Password', validators=[DataRequired(), Length(8, 150)]) - remember = BooleanField('Remember me') + remember = BooleanField('Remember me', description='Rember me on my next visit', render_kw={'descr_class': 'fw-bold text-decoration-line-through'}) submit = SubmitField() class ButtonForm(FlaskForm): username = StringField('Username', validators=[DataRequired(), Length(1, 20)]) - confirm = SwitchField('Confirmation') + confirm = SwitchField('Confirmation', description='Are you sure?', render_kw={'label_class': 'font-monospace text-decoration-underline'}) submit = SubmitField() delete = SubmitField() cancel = SubmitField() @@ -190,7 +206,9 @@ def test_form(): contact_form=ContactForm(), im_form=IMForm(), button_form=ButtonForm(), - example_form=ExampleForm() + example_form=ExampleForm(), + inline_form=ExampleFormInline(), + horizontal_form=ExampleFormHorizontal() ) diff --git a/examples/bootstrap5/templates/form.html b/examples/bootstrap5/templates/form.html index b0b7b0a1..3b7dc7bf 100644 --- a/examples/bootstrap5/templates/form.html +++ b/examples/bootstrap5/templates/form.html @@ -6,10 +6,10 @@

Example Form

 class ExampleForm(FlaskForm):
-    date = DateField(description="We'll never share your email with anyone else.")  # add help text with `description`
-    datetime = DateTimeField(render_kw={'placeholder': 'this is a placeholder'})  # add HTML attribute with `render_kw`
+    date = DateField(description='Your memorable date')  # add help text with `description`
+    datetime = DateTimeField(render_kw={'placeholder': 'this is a placeholder', 'class': 'fst-italic'})  # add HTML attribute with `render_kw`
     datetimelocal = DateTimeLocalField()
-    time = TimeField()
+    time = TimeField(description='This is private', render_kw={'descr_class': 'fs-1 text-decoration-underline'})
     month = MonthField()
     color = ColorField()
     floating = FloatField()
@@ -20,17 +20,25 @@ 

Example Form

url = URLField() telephone = TelField() image = FileField(render_kw={'class': 'my-class'}, validators=[Regexp('.+\.jpg$')]) # add your class - option = RadioField(choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')]) + option = RadioField(choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')], render_kw={'label_class': 'text-decoration-underline', 'radio_class': 'text-decoration-line-through'}) select = SelectField(choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')]) selectmulti = SelectMultipleField(choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')]) bio = TextAreaField() - search = SearchField() # will autocapitalize on mobile - title = StringField() # will not autocapitalize on mobile + search = SearchField() # will autocapitalize on mobile + title = StringField() # will not autocapitalize on mobile secret = PasswordField() remember = BooleanField('Remember me') submit = SubmitField()
{{ render_form(example_form) }} +

Inline form

+
{% raw %}{{ render_form(inline_form, form_type='inline') }}{% endraw %}
+ {{ render_form(inline_form, form_type='inline') }} + +

Horizontal form

+
{% raw %}{{ render_form(horizontal_form, form_type='horizontal') }}{% endraw %}
+ {{ render_form(horizontal_form, form_type='horizontal') }} +

Render a form with render_form

{% raw %}{{ render_form(form) }}{% endraw %}
{{ render_form(form) }} diff --git a/flask_bootstrap/templates/bootstrap5/form.html b/flask_bootstrap/templates/bootstrap5/form.html index e4d6e14b..f312f146 100644 --- a/flask_bootstrap/templates/bootstrap5/form.html +++ b/flask_bootstrap/templates/bootstrap5/form.html @@ -45,6 +45,23 @@ {% set form_group_classes = form_group_classes or config.BOOTSTRAP_FORM_GROUP_CLASSES %} + {# support for label_class and descr_class which are popped, to prevent they are added to input, but restored at the end of this macro for the next rendering #} + {%- set label_class = '' -%} + {%- if field.render_kw.label_class -%} + {% set label_class = field.render_kw.pop('label_class', '') -%} + {% set label_classes = ' ' + label_class -%} + {%- endif -%} + {%- set radio_class = '' -%} + {%- if field.render_kw.radio_class -%} + {% set radio_class = field.render_kw.pop('radio_class', '') -%} + {% set radio_classes = ' ' + radio_class -%} + {%- endif -%} + {%- set descr_class = '' -%} + {%- if field.render_kw.descr_class -%} + {% set descr_class = field.render_kw.pop('descr_class', '') -%} + {% set descr_classes = ' ' + descr_class -%} + {%- endif -%} + {# combine render_kw class or class/class_ argument with Bootstrap classes #} {% set render_kw_class = ' ' + field.render_kw.class if field.render_kw.class else '' %} {% set class = kwargs.pop('class', '') or kwargs.pop('class_', '') %} @@ -68,14 +85,14 @@ {%- else -%} {{ field(class="form-check-input%s" % extra_classes, **field_kwargs)|safe }} {%- endif %} - {{ field.label(class="form-check-label", for=field.id)|safe }} + {{ field.label(class="form-check-label%s" % label_classes, for=field.id)|safe }} {%- if field.errors %} {%- for error in field.errors %}
{{ error }}
{%- endfor %} {%- elif field.description -%} {% call _hz_form_wrap(horizontal_columns, form_type, required=required) %} - {{ field.description|safe }} + {{ field.description|safe }} {% endcall %} {%- endif %} @@ -85,12 +102,13 @@ this is just a hack for now, until I can think of something better #}
{%- if form_type == "inline" %} - {{ field.label(class="visually-hidden")|safe }} + {{ field.label(class="visually-hidden%s" % label_classes)|safe }} {% elif form_type == "horizontal" %} {{ field.label(class="col-form-label" + ( - " col-%s-%s" % horizontal_columns[0:2]))|safe }} + " col-%s-%s" % horizontal_columns[0:2]) + ( + "%s" % label_classes))|safe }} {%- else -%} - {{ field.label(class="form-label")|safe }} + {{ field.label(class="form-label%s" % label_classes)|safe }} {% endif %} {% if form_type == 'horizontal' %}
@@ -99,7 +117,7 @@ {% for item in field -%}
{{ item(class="form-check-input")|safe }} - {{ item.label(class="form-check-label", for=item.id)|safe }} + {{ item.label(class="form-check-label%s" % radio_classes, for=item.id)|safe }}
{% endfor %} {#% endcall %#} @@ -111,7 +129,7 @@
{{ error }}
{%- endfor %} {%- elif field.description -%} - {{ field.description|safe }} + {{ field.description|safe }} {%- endif %}
{%- elif field.type == 'SubmitField' -%} @@ -145,7 +163,7 @@
{%- if form_type == "inline" %} - {{ field.label(class="visually-hidden")|safe }} + {{ field.label(class="visually-hidden%s" % label_classes)|safe }} {%- if field.type in ['DecimalRangeField', 'IntegerRangeField'] %} {% if field.errors %} {{ field(class="form-range is-invalid%s" % extra_classes, **kwargs)|safe }} @@ -166,7 +184,9 @@ {% endif %} {%- endif %} {% elif form_type == "horizontal" %} - {{ field.label(class="col-form-label" + (" col-%s-%s" % horizontal_columns[0:2]))|safe }} + {{ field.label(class="col-form-label" + ( + " col-%s-%s" % horizontal_columns[0:2]) + ( + "%s" % label_classes))|safe }}
{%- if field.type in ['DecimalRangeField', 'IntegerRangeField'] %} {% if field.errors %} @@ -196,11 +216,11 @@ {%- endfor %} {%- elif field.description -%} {% call _hz_form_wrap(horizontal_columns, form_type, required=required) %} - {{ field.description|safe }} + {{ field.description|safe }} {% endcall %} {%- endif %} {%- else -%} - {{ field.label(class="form-label")|safe }} + {{ field.label(class="form-label%s" % label_classes)|safe }} {%- if field.type in ['DecimalRangeField', 'IntegerRangeField'] %} {% if field.errors %} {{ field(class="form-range is-invalid%s" % extra_classes, **kwargs)|safe }} @@ -225,11 +245,21 @@
{{ error }}
{%- endfor %} {%- elif field.description -%} - {{ field.description|safe }} + {{ field.description|safe }} {%- endif %} {%- endif %}
{% endif %} + + {%- if label_class -%} + {%- set _ = field.render_kw.update({'label_class': label_class}) -%} + {%- endif -%} + {%- if radio_class -%} + {%- set _ = field.render_kw.update({'radio_class': radio_class}) -%} + {%- endif -%} + {%- if descr_class -%} + {%- set _ = field.render_kw.update({'descr_class': descr_class}) -%} + {%- endif -%} {% endmacro %} {# valid form types are "basic", "inline" and "horizontal" #} diff --git a/tests/conftest.py b/tests/conftest.py index 267c596c..4fcd3f51 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ import pytest from flask import Flask, render_template_string from flask_wtf import FlaskForm -from wtforms import BooleanField, PasswordField, StringField, SubmitField, HiddenField +from wtforms import BooleanField, PasswordField, StringField, SubmitField, HiddenField, IntegerField, RadioField from wtforms.validators import DataRequired, Length @@ -14,11 +14,31 @@ class HelloForm(FlaskForm): submit = SubmitField() +class ClassForm(FlaskForm): + boolean = BooleanField('Bool label', description="Bool descr", + render_kw={'label_class': 'text-decoration-underline', + 'descr_class': 'text-decoration-line-through'}) + integer = IntegerField('Int label', description="Int descr", + render_kw={'label_class': 'text-decoration-underline', + 'descr_class': 'text-decoration-line-through'}) + option = RadioField('Rad label', + description='Rad descr', + choices=[('one', 'One'), ('two', 'Two')], + render_kw={'label_class': 'text-uppercase', + 'radio_class': 'text-decoration-line-through', + 'descr_class': 'text-decoration-underline'}) + + @pytest.fixture def hello_form(): return HelloForm +@pytest.fixture +def class_form(): + return ClassForm + + @pytest.fixture(autouse=True) def app(): app = Flask(__name__) diff --git a/tests/test_bootstrap5/test_render_form.py b/tests/test_bootstrap5/test_render_form.py index f2db153f..58aacbf7 100644 --- a/tests/test_bootstrap5/test_render_form.py +++ b/tests/test_bootstrap5/test_render_form.py @@ -136,3 +136,97 @@ def test_config(): data = response.get_data(as_text=True) assert 'row row-cols-lg-auto g-3 align-items-center' not in data assert 'custom-inline-classes' in data + + +# test render label_class, radio_class and descr_class +def test_class(app, client, class_form): + + @app.route('/class') + def test_class(): + form = class_form() + return render_template_string(''' + {% from 'bootstrap5/form.html' import render_form %} + {{ render_form(form) }} + ''', form=form) + + response = client.get('/class') + data = response.get_data(as_text=True) + + # render_field form.html line 88 + assert '' in data + # render_field form.html line 95 + assert 'Bool descr' in data + + # render_field form.html line 223 + assert '' in data + # render_field form.html line 248 + assert 'Int descr' in data + + # render_field form.html line 111 + assert '' in data + # render_field form.html line 120 + assert '' in data + # render_field form.html line 132 + assert 'Rad descr' in data + + +def test_class_inline(app, client, class_form): + + @app.route('/class_inline') + def test_class_inline(): + form = class_form() + return render_template_string(''' + {% from 'bootstrap5/form.html' import render_form %} + {{ render_form(form, form_type='inline') }} + ''', form=form) + + response = client.get('/class_inline') + data = response.get_data(as_text=True) + + # render_field form.html line 88, repeat from other test + assert '' in data + # render_field form.html line 95, repeat from other test + assert 'Bool descr' in data + + # render_field form.html line 166, probably not displayed + assert '' in data + # render_field form.html not rendered description + assert '">Int descr' not in data + + # render_field form.html line 105, probabaly not displayed + assert '' in data + # render_field form.html line 120, repeat from other test + assert '' in data + # render_field form.html line 132, repeat from other test + assert 'Rad descr' in data + + +def test_class_horizontal(app, client, class_form): + + @app.route('/class_horizontal') + def test_class_horizontal(): + form = class_form() + return render_template_string(''' + {% from 'bootstrap5/form.html' import render_form %} + {{ render_form(form, form_type='horizontal') }} + ''', form=form) + + response = client.get('/class_horizontal') + data = response.get_data(as_text=True) + + # render_field form.html line 88, repeat from other test + assert '' in data + # render_field form.html line 95, repeat from other test + assert 'Bool descr' in data + + # render_field form.html line 189 + assert '' in data + # render_field form.html line 219 + assert 'Int descr' in data + + # render_field form.html line 109 + assert '' in data + # render_field form.html line 120, repeat from other test + assert '' in data + # render_field form.html line 132, repeat from other test + assert 'Rad descr' in data From 1d8548836274b681d247ce2e29c76c95c6e51867 Mon Sep 17 00:00:00 2001 From: Pander Date: Sun, 9 Mar 2025 22:34:22 +0100 Subject: [PATCH 2/2] refactored descr_class to description_class --- docs/advanced.rst | 4 ++-- examples/bootstrap5/app.py | 10 ++++----- examples/bootstrap5/templates/form.html | 2 +- .../templates/bootstrap5/form.html | 22 +++++++++---------- tests/conftest.py | 6 ++--- tests/test_bootstrap5/test_render_form.py | 2 +- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/docs/advanced.rst b/docs/advanced.rst index eb324437..a4c0dadf 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -164,8 +164,8 @@ example application. Rendering Description ~~~~~~~~~~~~~~~~~~~~~ -Use ``render_kw={'descr_class': '... ...'}`` for controlling the rendering of a -field's description. See also the example application. +Use ``render_kw={'description_class': '... ...'}`` for controlling the +rendering of a field's description. See also the example application. .. _bootswatch_theme: diff --git a/examples/bootstrap5/app.py b/examples/bootstrap5/app.py index 63d7be35..732beeea 100644 --- a/examples/bootstrap5/app.py +++ b/examples/bootstrap5/app.py @@ -37,7 +37,7 @@ class ExampleForm(FlaskForm): date = DateField(description='Your memorable date') # add help text with `description` datetime = DateTimeField(render_kw={'placeholder': 'this is a placeholder', 'class': 'text-decoration-line-through'}) # add HTML attribute with `render_kw` datetime_local = DateTimeLocalField() - time = TimeField(description='This is private', render_kw={'descr_class': 'fst-italic text-decoration-underline'}) + time = TimeField(description='This is private', render_kw={'description_class': 'fst-italic text-decoration-underline'}) month = MonthField() color = ColorField() floating = FloatField() @@ -62,15 +62,15 @@ class ExampleForm(FlaskForm): class ExampleFormInline(FlaskForm): """An example inline form.""" floating = FloatField(description='a float', render_kw={'label_class': 'text-decoration-underline'}) - integer = IntegerField(description='an int', render_kw={'descr_class': 'text-decoration-line-through'}) - option = RadioField(description='Choose one', choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')], render_kw={'radio_class': 'text-decoration-line-through', 'descr_class': 'fw-bold'}) + integer = IntegerField(description='an int', render_kw={'description_class': 'text-decoration-line-through'}) + option = RadioField(description='Choose one', choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')], render_kw={'radio_class': 'text-decoration-line-through', 'description_class': 'fw-bold'}) submit = SubmitField() class ExampleFormHorizontal(FlaskForm): """An example horizontal form.""" floating = FloatField(description='a float', render_kw={'label_class': 'text-decoration-underline'}) - integer = IntegerField(description='an int', render_kw={'descr_class': 'text-decoration-line-through'}) + integer = IntegerField(description='an int', render_kw={'description_class': 'text-decoration-line-through'}) option = RadioField(description='choose 1', choices=[('dog', 'Dog'), ('cat', 'Cat'), ('bird', 'Bird'), ('alien', 'Alien')], render_kw={'label_class': 'text-decoration-underline'}) submit = SubmitField() @@ -78,7 +78,7 @@ class ExampleFormHorizontal(FlaskForm): class HelloForm(FlaskForm): username = StringField('Username', validators=[DataRequired(), Length(1, 20)]) password = PasswordField('Password', validators=[DataRequired(), Length(8, 150)]) - remember = BooleanField('Remember me', description='Rember me on my next visit', render_kw={'descr_class': 'fw-bold text-decoration-line-through'}) + remember = BooleanField('Remember me', description='Rember me on my next visit', render_kw={'description_class': 'fw-bold text-decoration-line-through'}) submit = SubmitField() diff --git a/examples/bootstrap5/templates/form.html b/examples/bootstrap5/templates/form.html index 3b7dc7bf..9164c689 100644 --- a/examples/bootstrap5/templates/form.html +++ b/examples/bootstrap5/templates/form.html @@ -9,7 +9,7 @@

Example Form

date = DateField(description='Your memorable date') # add help text with `description` datetime = DateTimeField(render_kw={'placeholder': 'this is a placeholder', 'class': 'fst-italic'}) # add HTML attribute with `render_kw` datetimelocal = DateTimeLocalField() - time = TimeField(description='This is private', render_kw={'descr_class': 'fs-1 text-decoration-underline'}) + time = TimeField(description='This is private', render_kw={'description_class': 'fs-1 text-decoration-underline'}) month = MonthField() color = ColorField() floating = FloatField() diff --git a/flask_bootstrap/templates/bootstrap5/form.html b/flask_bootstrap/templates/bootstrap5/form.html index f312f146..be72832a 100644 --- a/flask_bootstrap/templates/bootstrap5/form.html +++ b/flask_bootstrap/templates/bootstrap5/form.html @@ -45,7 +45,7 @@ {% set form_group_classes = form_group_classes or config.BOOTSTRAP_FORM_GROUP_CLASSES %} - {# support for label_class and descr_class which are popped, to prevent they are added to input, but restored at the end of this macro for the next rendering #} + {# support for label_class, radio_class and description_class which are popped, to prevent they are added to input, but restored at the end of this macro for the next rendering #} {%- set label_class = '' -%} {%- if field.render_kw.label_class -%} {% set label_class = field.render_kw.pop('label_class', '') -%} @@ -56,10 +56,10 @@ {% set radio_class = field.render_kw.pop('radio_class', '') -%} {% set radio_classes = ' ' + radio_class -%} {%- endif -%} - {%- set descr_class = '' -%} - {%- if field.render_kw.descr_class -%} - {% set descr_class = field.render_kw.pop('descr_class', '') -%} - {% set descr_classes = ' ' + descr_class -%} + {%- set description_class = '' -%} + {%- if field.render_kw.description_class -%} + {% set description_class = field.render_kw.pop('description_class', '') -%} + {% set description_classes = ' ' + description_class -%} {%- endif -%} {# combine render_kw class or class/class_ argument with Bootstrap classes #} @@ -92,7 +92,7 @@ {%- endfor %} {%- elif field.description -%} {% call _hz_form_wrap(horizontal_columns, form_type, required=required) %} - {{ field.description|safe }} + {{ field.description|safe }} {% endcall %} {%- endif %}
@@ -129,7 +129,7 @@
{{ error }}
{%- endfor %} {%- elif field.description -%} - {{ field.description|safe }} + {{ field.description|safe }} {%- endif %}
{%- elif field.type == 'SubmitField' -%} @@ -216,7 +216,7 @@ {%- endfor %} {%- elif field.description -%} {% call _hz_form_wrap(horizontal_columns, form_type, required=required) %} - {{ field.description|safe }} + {{ field.description|safe }} {% endcall %} {%- endif %} {%- else -%} @@ -245,7 +245,7 @@
{{ error }}
{%- endfor %} {%- elif field.description -%} - {{ field.description|safe }} + {{ field.description|safe }} {%- endif %} {%- endif %} @@ -257,8 +257,8 @@ {%- if radio_class -%} {%- set _ = field.render_kw.update({'radio_class': radio_class}) -%} {%- endif -%} - {%- if descr_class -%} - {%- set _ = field.render_kw.update({'descr_class': descr_class}) -%} + {%- if description_class -%} + {%- set _ = field.render_kw.update({'description_class': description_class}) -%} {%- endif -%} {% endmacro %} diff --git a/tests/conftest.py b/tests/conftest.py index 4fcd3f51..1ad4cd93 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,16 +17,16 @@ class HelloForm(FlaskForm): class ClassForm(FlaskForm): boolean = BooleanField('Bool label', description="Bool descr", render_kw={'label_class': 'text-decoration-underline', - 'descr_class': 'text-decoration-line-through'}) + 'description_class': 'text-decoration-line-through'}) integer = IntegerField('Int label', description="Int descr", render_kw={'label_class': 'text-decoration-underline', - 'descr_class': 'text-decoration-line-through'}) + 'description_class': 'text-decoration-line-through'}) option = RadioField('Rad label', description='Rad descr', choices=[('one', 'One'), ('two', 'Two')], render_kw={'label_class': 'text-uppercase', 'radio_class': 'text-decoration-line-through', - 'descr_class': 'text-decoration-underline'}) + 'description_class': 'text-decoration-underline'}) @pytest.fixture diff --git a/tests/test_bootstrap5/test_render_form.py b/tests/test_bootstrap5/test_render_form.py index 58aacbf7..06c0136d 100644 --- a/tests/test_bootstrap5/test_render_form.py +++ b/tests/test_bootstrap5/test_render_form.py @@ -138,7 +138,7 @@ def test_config(): assert 'custom-inline-classes' in data -# test render label_class, radio_class and descr_class +# test render label_class, radio_class and description_class def test_class(app, client, class_form): @app.route('/class')