Skip to content

Commit

Permalink
Merge pull request #420 from jonashaag/develop
Browse files Browse the repository at this point in the history
Add 'label' override for {% bootstrap_field %}
  • Loading branch information
dyve committed Oct 27, 2017
2 parents e4559ab + 61359fb commit 652cffd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ development (in progress)
* Mention `django-bootstrap4 <https://github.com/zostera/django-bootstrap4/>`_ in README
* Rewrite `tox` test matrix to focus on Django releases rather than Python versions
* Add tests for Django master branch (>= 2)
* Add `label` override for `{% bootstrap_field %}`


9.0.0 (2017-07-11)
Expand Down
8 changes: 5 additions & 3 deletions bootstrap3/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,14 @@ def __init__(self, field, *args, **kwargs):
self.field_help = text_value(mark_safe(field.help_text)) if self.show_help and field.help_text else ''
self.field_errors = [conditional_escape(text_value(error)) for error in field.errors]

self.label = kwargs.get('label', field.label)

if 'placeholder' in kwargs:
# Find the placeholder in kwargs, even if it's empty
self.placeholder = kwargs['placeholder']
elif get_bootstrap_setting('set_placeholder'):
# If not found, see if we set the label
self.placeholder = field.label
self.placeholder = self.label
else:
# Or just set it to empty
self.placeholder = ''
Expand Down Expand Up @@ -390,7 +392,7 @@ def list_to_class(self, html, klass):
def put_inside_label(self, html):
content = '{field} {label}'.format(
field=html,
label=self.field.label,
label=self.label,
)
return render_label(
content=mark_safe(content),
Expand Down Expand Up @@ -503,7 +505,7 @@ def get_label(self):
if isinstance(self.widget, CheckboxInput):
label = None
else:
label = self.field.label
label = self.label
if self.layout == 'horizontal' and not label:
return mark_safe('&#160;')
return label
Expand Down
5 changes: 4 additions & 1 deletion bootstrap3/templatetags/bootstrap3.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,10 @@ def bootstrap_field(*args, **kwargs):
* ``'large'``
placeholder
Sets the placeholder text of a textbox
Set/overwrite the field's placeholder.
label
Overwrite the field's label.
horizontal_label_class
Class used on the label when the ``layout`` is set to ``horizontal``.
Expand Down
21 changes: 20 additions & 1 deletion bootstrap3/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.contrib.messages import constants as DEFAULT_MESSAGE_LEVELS
from django.forms.formsets import formset_factory
from django.template import engines
from django.test import TestCase
from django.test import TestCase, override_settings

from .bootstrap import DBS3_SET_REQUIRED_SET_DISABLED
from .exceptions import BootstrapError
Expand Down Expand Up @@ -579,6 +579,25 @@ def test_attributes_consistency(self):
attrs = form.fields['addon'].widget.attrs.copy()
self.assertEqual(attrs, form.fields['addon'].widget.attrs)

def test_placeholder(self):
res = render_template_with_form('{% bootstrap_field form.sender %}')
self.assertIn('placeholder="Sender', res)

def test_overwrite_placeholder(self):
res = render_template_with_form('{% bootstrap_field form.sender placeholder="foo" %}')
self.assertIn('placeholder="foo', res)

# If set_placeholder is set, also consider label override for placeholder
res = render_template_with_form('{% bootstrap_field form.sender label="foo" %}')
self.assertNotIn('Sender', res)
self.assertIn('placeholder="foo', res)
self.assertIn('foo</label>', res)

def test_overwrite_label(self):
res = render_template_with_form('{% bootstrap_field form.sender label="foo" %}')
self.assertNotIn('Sender', res)
self.assertIn('foo', res)


class ComponentsTest(TestCase):
def test_icon(self):
Expand Down
3 changes: 2 additions & 1 deletion docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ The ``BOOTSTRAP3`` dict variable contains these settings and defaults:
# Set HTML disabled attribute on disabled fields, for Django <= 1.8 only
'set_disabled': False,
# Set placeholder attributes to label if no placeholder is provided
# Set placeholder attributes to label if no placeholder is provided.
# This also considers the 'label' option of {% bootstrap_field %} tags.
'set_placeholder': True,
# Class to indicate required (better to set this in your Django form)
Expand Down
2 changes: 1 addition & 1 deletion testsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
#'django.contrib.gis',

# We test this one
'bootstrap3',
Expand Down

0 comments on commit 652cffd

Please sign in to comment.