Skip to content

Commit 90d34bc

Browse files
committed
bootstrap3: make the bound class configurable
Both in settings and as template tag parameter
1 parent ec129b1 commit 90d34bc

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

bootstrap3/bootstrap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'set_placeholder': True,
2121
'form_required_class': '',
2222
'form_error_class': 'has-error',
23+
'form_bound_class': 'has-success',
2324
'formset_renderers':{
2425
'default': 'bootstrap3.renderers.FormsetRenderer',
2526
},

bootstrap3/renderers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def __init__(self, field, *args, **kwargs):
184184
# These are set in Django or in the global BOOTSTRAP3 settings, and they can be overwritten in the template
185185
error_css_class = kwargs.get('error_css_class', '')
186186
required_css_class = kwargs.get('required_css_class', '')
187+
bound_css_class = kwargs.get('bound_css_class', '')
187188
if error_css_class:
188189
self.form_error_class = error_css_class
189190
else:
@@ -193,6 +194,10 @@ def __init__(self, field, *args, **kwargs):
193194
else:
194195
self.form_required_class = getattr(field.form, 'required_css_class',
195196
get_bootstrap_setting('form_required_class'))
197+
if bound_css_class:
198+
self.form_bound_class = bound_css_class
199+
else:
200+
self.form_bound_class = getattr(field.form, 'bound_css_class', get_bootstrap_setting('form_bound_class'))
196201

197202
def restore_widget_attrs(self):
198203
self.widget.attrs = self.initial_attrs
@@ -353,7 +358,7 @@ def get_form_group_class(self):
353358
form_group_class = add_css_class(
354359
form_group_class, self.form_error_class)
355360
elif self.field.form.is_bound:
356-
form_group_class = add_css_class(form_group_class, 'has-success')
361+
form_group_class = add_css_class(form_group_class, self.form_bound_class)
357362
if self.layout == 'horizontal':
358363
form_group_class = add_css_class(form_group_class, self.get_size_class(prefix='form-group'))
359364
return form_group_class

bootstrap3/tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ def test_error_class(self):
145145
res = render_template('{% bootstrap_form form %}', form=form)
146146
self.assertIn('bootstrap3-err', res)
147147

148+
def test_bound_class(self):
149+
form = TestForm({'sender': 'sender'})
150+
res = render_template('{% bootstrap_form form %}', form=form)
151+
self.assertIn('bootstrap3-bound', res)
152+
148153

149154
class TemplateTest(TestCase):
150155
def test_empty_template(self):

docs/settings.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ The ``BOOTSTRAP3`` dict variable is contains these settings and defaults:
5151
'form_required_class': '',
5252
5353
# Class to indicate error (better to set this in your Django form)
54-
'form_error_class': '',
54+
'form_error_class': 'has-error',
55+
56+
# Class to indicate the field is bound (better to set this in your Django form)
57+
'form_bound_class': 'has-success',
5558
5659
# Renderers (only set these if you have studied the source and understand the inner workings)
5760
'formset_renderers':{

testsettings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'javascript_in_head': True,
1414
'form_required_class': 'bootstrap3-req',
1515
'form_error_class': 'bootstrap3-err',
16+
'form_bound_class': 'bootstrap3-bound',
1617
}
1718

1819
SECRET_KEY = 'bootstrap3isawesome'

0 commit comments

Comments
 (0)