Skip to content

Commit eb7aeea

Browse files
author
Dylan Verheul
committed
Merge pull request zostera#164 from dyve/xrmx-fixformgroupclasses
Pull in changes for success / bound form class handling
2 parents feec4b5 + 0d62642 commit eb7aeea

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

bootstrap3/bootstrap.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
'set_required': True,
2020
'set_placeholder': True,
2121
'required_css_class': '',
22-
'error_css_class': '',
22+
'error_css_class': 'has-error',
23+
'success_css_class': 'has-success',
2324
'formset_renderers': {
2425
'default': 'bootstrap3.renderers.FormsetRenderer',
2526
},

bootstrap3/renderers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def __init__(self, field, *args, **kwargs):
201201
# These are set in Django or in the global BOOTSTRAP3 settings, and they can be overwritten in the template
202202
error_css_class = kwargs.get('error_css_class', '')
203203
required_css_class = kwargs.get('required_css_class', '')
204+
bound_css_class = kwargs.get('bound_css_class', '')
204205
if error_css_class:
205206
self.error_css_class = error_css_class
206207
else:
@@ -210,6 +211,11 @@ def __init__(self, field, *args, **kwargs):
210211
else:
211212
self.required_css_class = getattr(field.form, 'required_css_class',
212213
get_bootstrap_setting('required_css_class'))
214+
if bound_css_class:
215+
self.success_css_class = bound_css_class
216+
else:
217+
self.success_css_class = getattr(field.form, 'bound_css_class', get_bootstrap_setting('success_css_class'))
218+
213219
# Handle form.empty_permitted
214220
if self.field.form.empty_permitted:
215221
self.set_required = False
@@ -377,7 +383,7 @@ def get_form_group_class(self):
377383
if self.field_errors:
378384
form_group_class = add_css_class(form_group_class, 'has-error')
379385
elif self.field.form.is_bound:
380-
form_group_class = add_css_class(form_group_class, 'has-success')
386+
form_group_class = add_css_class(form_group_class, self.success_css_class)
381387
if self.layout == 'horizontal':
382388
form_group_class = add_css_class(form_group_class, self.get_size_class(prefix='form-group'))
383389
return form_group_class

bootstrap3/tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ def test_settings_filter(self):
136136
'{% load bootstrap3 %}{% if "javascript_in_head"|bootstrap_setting %}head{% else %}body{% endif %}')
137137
self.assertEqual(res.strip(), 'head')
138138

139+
def test_required_class(self):
140+
form = TestForm()
141+
res = render_template('{% bootstrap_form form %}', form=form)
142+
self.assertIn('bootstrap3-req', res)
143+
144+
def test_error_class(self):
145+
form = TestForm({})
146+
res = render_template('{% bootstrap_form form %}', form=form)
147+
self.assertIn('bootstrap3-err', res)
148+
149+
def test_bound_class(self):
150+
form = TestForm({'sender': 'sender'})
151+
res = render_template('{% bootstrap_form form %}', form=form)
152+
self.assertIn('bootstrap3-bound', res)
153+
139154

140155
class TemplateTest(TestCase):
141156
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
'required_css_class': '',
5252
5353
# Class to indicate error (better to set this in your Django form)
54-
'error_css_class': '',
54+
'error_css_class': 'has-error',
55+
56+
# Class to indicate success, meaning the field has valid input (better to set this in your Django form)
57+
'success_css_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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
BOOTSTRAP3 = {
1313
'javascript_in_head': True,
1414
'required_css_class': 'bootstrap3-req',
15+
'error_css_class': 'bootstrap3-err',
16+
'success_css_class': 'bootstrap3-bound',
1517
}
1618

1719
SECRET_KEY = 'bootstrap3isawesome'

0 commit comments

Comments
 (0)