Skip to content

Commit 87e91ef

Browse files
committed
Renaming of parameters, changing defaults, other fixes for error_types
1 parent 2767860 commit 87e91ef

File tree

7 files changed

+114
-38
lines changed

7 files changed

+114
-38
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Contributors
1717
* Caio Ariede <[email protected]>
1818
* Fabio C. Barrionuevo da Luz <[email protected]>
1919
* Fabio Perfetti <[email protected]>
20+
* Irving Ckam <https://github.com/ickam>
2021
* Jay Pipes <[email protected]>
2122
* Jonas Hagstedt <[email protected]>
2223
* Jordan Starcher <[email protected]>

HISTORY.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Development (in progress)
88
+++++++++++++++++++++++++
99

1010
* Renamed requirements-dev.txt back to requirements.txt because that suits ReadTheDocs better
11-
* Added 'errors_type' support on bootstrap3_form (thanks @mkoistinen)
11+
* Added 'error_types' support on bootstrap3_form (thanks @mkoistinen and @ickam)
1212

1313

1414
8.2.3 (2017-05-05)

bootstrap3/forms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ def render_form(form, **kwargs):
5353
return renderer_cls(form, **kwargs).render()
5454

5555

56-
def render_form_errors(form, type='all', **kwargs):
56+
def render_form_errors(form, error_types='non_field_errors', **kwargs):
5757
"""
5858
Render form errors to a Bootstrap layout
5959
"""
6060
renderer_cls = get_form_renderer(**kwargs)
61-
return renderer_cls(form, **kwargs).render_errors(type)
61+
return renderer_cls(form, **kwargs).render_errors(error_types)
6262

6363

6464
def render_field(field, **kwargs):

bootstrap3/renderers.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def __init__(self, form, *args, **kwargs):
166166
if DBS3_SET_REQUIRED_SET_DISABLED and self.form.empty_permitted:
167167
self.set_required = False
168168

169-
self.errors_type = kwargs.get('errors_type', 'all')
169+
self.error_types = kwargs.get('error_types', 'non_field_errors')
170170
self.error_css_class = kwargs.get('error_css_class', None)
171171
self.required_css_class = kwargs.get('required_css_class', None)
172172
self.bound_css_class = kwargs.get('bound_css_class', None)
@@ -202,14 +202,16 @@ def get_fields_errors(self):
202202
form_errors += field.errors
203203
return form_errors
204204

205-
def render_errors(self, type='all'):
205+
def render_errors(self, error_types='all'):
206206
form_errors = []
207-
if type == 'all':
207+
if error_types == 'all':
208208
form_errors = self.get_fields_errors() + self.form.non_field_errors()
209-
elif type == 'fields':
209+
elif error_types == 'field_errors':
210210
form_errors = self.get_fields_errors()
211-
elif type == 'non_fields':
211+
elif error_types == 'non_field_errors':
212212
form_errors = self.form.non_field_errors()
213+
elif error_types and error_types != 'none':
214+
raise Exception('Illegal value "{}" for error_types.')
213215

214216
if form_errors:
215217
return render_template_file(
@@ -218,14 +220,14 @@ def render_errors(self, type='all'):
218220
'errors': form_errors,
219221
'form': self.form,
220222
'layout': self.layout,
221-
'type': type,
223+
'error_types': error_types,
222224
}
223225
)
224226

225227
return ''
226228

227229
def _render(self):
228-
return self.render_errors(self.errors_type) + self.render_fields()
230+
return self.render_errors(self.error_types) + self.render_fields()
229231

230232

231233
class FieldRenderer(BaseRenderer):

bootstrap3/templatetags/bootstrap3.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,12 @@ def bootstrap_form(*args, **kwargs):
318318
A list of field names (comma separated) that should not be rendered
319319
E.g. exclude=subject,bcc
320320
321-
errors_type
321+
error_types
322322
This controls the types of errors that are rendered above the form.
323-
Choices are: "all", "fields", "non_fields" or "none". This will not
323+
Choices are: "all", "field_errors", "non_field_errors" or "none". This will not
324324
affect the display of errors on the fields themselves.
325325
326-
Default is "all".
326+
Default is "non_field_errors".
327327
328328
See bootstrap_field_ for other arguments
329329
@@ -352,16 +352,16 @@ def bootstrap_form_errors(*args, **kwargs):
352352
form
353353
The form that is to be rendered
354354
355-
type
355+
error_types
356356
Control which type of errors should be rendered.
357357
358358
One of the following values:
359359
360360
* ``'all'``
361-
* ``'fields'``
362-
* ``'non_fields'``
361+
* ``'field_errors'``
362+
* ``'non_field_errors'``
363363
364-
:default: ``'all'``
364+
:default: ``'non_field_errors'``
365365
366366
layout
367367
Context value that is available in the template ``bootstrap3/form_errors.html`` as ``layout``.

bootstrap3/tests.py

Lines changed: 93 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@
3838
)
3939

4040

41+
class SmallTestForm(forms.Form):
42+
sender = forms.EmailField(
43+
label='Sender © unicode',
44+
help_text='E.g., "[email protected]"')
45+
subject = forms.CharField(
46+
max_length=100,
47+
help_text='my_help_text',
48+
required=True,
49+
widget=forms.TextInput(attrs={'placeholder': 'placeholdertest'}),
50+
)
51+
52+
def clean(self):
53+
cleaned_data = super(SmallTestForm, self).clean()
54+
raise forms.ValidationError(
55+
"This error was added to show the non field errors styling.")
56+
return cleaned_data
57+
58+
4159
class TestForm(forms.Form):
4260
"""
4361
Form with a variety of widgets to test bootstrap3 rendering.
@@ -220,30 +238,26 @@ def test_bootstrap_css_tag(self):
220238
res
221239
)
222240

241+
def test_settings_filter(self):
242+
res = render_template_with_form('{{ "required_css_class"|bootstrap_setting }}')
243+
self.assertEqual(res.strip(), 'bootstrap3-req')
244+
res = render_template_with_form('{% if "javascript_in_head"|bootstrap_setting %}head{% else %}body{% endif %}')
245+
self.assertEqual(res.strip(), 'head')
223246

224-
def test_settings_filter(self):
225-
res = render_template_with_form('{{ "required_css_class"|bootstrap_setting }}')
226-
self.assertEqual(res.strip(), 'bootstrap3-req')
227-
res = render_template_with_form('{% if "javascript_in_head"|bootstrap_setting %}head{% else %}body{% endif %}')
228-
self.assertEqual(res.strip(), 'head')
229-
230-
231-
def test_required_class(self):
232-
form = TestForm()
233-
res = render_template_with_form('{% bootstrap_form form %}', {'form': form})
234-
self.assertIn('bootstrap3-req', res)
235-
236-
237-
def test_error_class(self):
238-
form = TestForm({})
239-
res = render_template_with_form('{% bootstrap_form form %}', {'form': form})
240-
self.assertIn('bootstrap3-err', res)
247+
def test_required_class(self):
248+
form = TestForm()
249+
res = render_template_with_form('{% bootstrap_form form %}', {'form': form})
250+
self.assertIn('bootstrap3-req', res)
241251

252+
def test_error_class(self):
253+
form = TestForm({})
254+
res = render_template_with_form('{% bootstrap_form form %}', {'form': form})
255+
self.assertIn('bootstrap3-err', res)
242256

243-
def test_bound_class(self):
244-
form = TestForm({'sender': 'sender'})
245-
res = render_template_with_form('{% bootstrap_form form %}', {'form': form})
246-
self.assertIn('bootstrap3-bound', res)
257+
def test_bound_class(self):
258+
form = TestForm({'sender': 'sender'})
259+
res = render_template_with_form('{% bootstrap_form form %}', {'form': form})
260+
self.assertIn('bootstrap3-bound', res)
247261

248262

249263
class TemplateTest(TestCase):
@@ -379,6 +393,64 @@ def test_bound_class(self):
379393
)
380394
self.assertNotIn('bootstrap3-bound', res)
381395

396+
def test_error_types(self):
397+
form = SmallTestForm({'sender': 'sender'})
398+
399+
pattern = re.compile(r'\s')
400+
401+
res = render_template_with_form(
402+
'{% bootstrap_form form error_types="all" %}',
403+
{'form': form}
404+
)
405+
expected = """
406+
<div class="alert alert-danger alert-dismissable alert-link">
407+
<button class="close" type="button" data-dismiss="alert" aria-hidden="true">&#215;</button>
408+
Enter a valid email address.<br>
409+
This field is required.<br>
410+
This error was added to show the non field errors styling.
411+
</div>
412+
"""
413+
self.assertIn(
414+
re.sub(pattern, '', expected),
415+
re.sub(pattern, '', res)
416+
)
417+
418+
res = render_template_with_form(
419+
'{% bootstrap_form form error_types="non_field_errors" %}',
420+
{'form': form}
421+
)
422+
expected = """
423+
<div class="alert alert-danger alert-dismissable alert-link">
424+
<button class="close" type="button" data-dismiss="alert" aria-hidden="true">&#215;</button>
425+
This error was added to show the non field errors styling.
426+
</div>
427+
"""
428+
self.assertIn(
429+
re.sub(pattern, '', expected),
430+
re.sub(pattern, '', res)
431+
)
432+
res2 = render_template_with_form(
433+
'{% bootstrap_form form %}',
434+
{'form': form}
435+
)
436+
self.assertEqual(res, res2)
437+
438+
res = render_template_with_form(
439+
'{% bootstrap_form form error_types="field_errors" %}',
440+
{'form': form}
441+
)
442+
expected = """
443+
<div class="alert alert-danger alert-dismissable alert-link">
444+
<button class="close" type="button" data-dismiss="alert" aria-hidden="true">&#215;</button>
445+
Enter a valid email address.<br>
446+
This field is required.
447+
</div>
448+
"""
449+
self.assertIn(
450+
re.sub(pattern, '', expected),
451+
re.sub(pattern, '', res)
452+
)
453+
382454

383455
class FieldTest(TestCase):
384456
def test_illegal_field(self):

demo/demo/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
'django.contrib.sites',
117117
'django.contrib.messages',
118118
'django.contrib.staticfiles',
119+
'django.contrib.admin',
119120

120121
'bootstrap3',
121122
'demo',

0 commit comments

Comments
 (0)