Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding tests and fixing some flaws for error_text_inline #GH-87

* Making `error_text_inline` play well with some layout objects
templates that were not including `help_text_and_errors.html`.
* Making `error_text_inline` play well with `help_text_inline`, order
matters for CSS.
* Adding tests for all these helper attributes.
  • Loading branch information...
commit a90ed48faa775309f6b82771b292fe52f0e75349 1 parent 6ac6daf
@maraujop authored
View
6 crispy_forms/templates/bootstrap/layout/appended_prepended_text.html
@@ -10,13 +10,11 @@
<div class="controls">
<div class="input-prepend input-append">
- {% spaceless %}
{% if crispy_prepended_text %}<span class="add-on {% if active %}active{% endif %}">{{ crispy_prepended_text|safe }}</span>{% endif %}
{% crispy_field field %}
{% if crispy_appended_text %}<span class="add-on {% if active %}active{% endif %}">{{ crispy_appended_text|safe }}</span>{% endif %}
- {% endspaceless %}
- {% include 'bootstrap/layout/field_errors.html' %}
</div>
- {% include 'bootstrap/layout/help_text.html' %}
+
+ {% include 'bootstrap/layout/help_text_and_errors.html' %}
</div>
</div>
View
3  crispy_forms/templates/bootstrap/layout/appended_text.html
@@ -12,9 +12,8 @@
<div class="input-append">
{% crispy_field field %}
<span class="add-on {% if active %}active{% endif %}">{{ crispy_appended_text|safe }}</span>
- {% include 'bootstrap/layout/field_errors.html' %}
</div>
- {% include 'bootstrap/layout/help_text.html' %}
+ {% include 'bootstrap/layout/help_text_and_errors.html' %}
</div>
</div>
View
9 crispy_forms/templates/bootstrap/layout/help_text_and_errors.html
@@ -1,6 +1,13 @@
+{% if help_text_inline and not error_text_inline %}
+ {% include 'bootstrap/layout/help_text.html' %}
+{% endif %}
+
{% if error_text_inline %}
{% include 'bootstrap/layout/field_errors.html' %}
{% else %}
{% include 'bootstrap/layout/field_errors_block.html' %}
{% endif %}
-{% include 'bootstrap/layout/help_text.html' %}
+
+{% if not help_text_inline %}
+ {% include 'bootstrap/layout/help_text.html' %}
+{% endif %}
View
4 crispy_forms/templates/bootstrap/layout/prepended_text.html
@@ -12,8 +12,8 @@
<div class="input-prepend">
<span class="add-on {% if active %}active{% endif %}">{{ crispy_prepended_text|safe }}</span>
{% crispy_field field %}
- {% include 'bootstrap/layout/field_errors.html' %}
</div>
- {% include 'bootstrap/layout/help_text.html' %}
+
+ {% include 'bootstrap/layout/help_text_and_errors.html' %}
</div>
</div>
View
1  crispy_forms/templatetags/crispy_forms_tags.py
@@ -151,6 +151,7 @@ def get_response_dict(self, helper, context, is_formset):
'is_formset': is_formset,
'%s_attrs' % form_type: attrs.get('attrs', ''),
'flat_attrs': attrs.get('flat_attrs', ''),
+ 'error_text_inline': attrs.get('error_text_inline', True),
}
# Handles custom attributes added to helpers
View
2  crispy_forms/tests/forms.py
@@ -5,7 +5,7 @@
class TestForm(forms.Form):
is_company = forms.CharField(label="company", required=False, widget=forms.CheckboxInput())
- email = forms.EmailField(label="email", max_length=30, required=True, widget=forms.TextInput())
+ email = forms.EmailField(label="email", max_length=30, required=True, widget=forms.TextInput(), help_text="Insert your email")
password1 = forms.CharField(label="password", max_length=30, required=True, widget=forms.PasswordInput())
password2 = forms.CharField(label="re-enter password", max_length=30, required=True, widget=forms.PasswordInput())
first_name = forms.CharField(label="first name", max_length=5, required=True, widget=forms.TextInput())
View
55 crispy_forms/tests/tests.py
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
+import re
+
import django
from django import forms
from django.conf import settings
@@ -256,7 +258,58 @@ def test_html5_required(self):
form.helper = FormHelper()
form.helper.html5_required = False
html = render_crispy_form(form)
- self.assertEqual(html.count('required="required"'), 0)
+
+ def test_error_text_inline(self):
+ form = TestForm({'email': 'invalidemail'})
+ form.helper = FormHelper()
+ layout = Layout(
+ AppendedText('first_name', 'wat'),
+ PrependedText('email', '@'),
+ AppendedPrependedText('last_name', '@', 'wat'),
+ )
+ form.helper.layout = layout
+ form.is_valid()
+ html = render_crispy_form(form)
+
+ matches = re.findall('<span id="error_\d_\w*" class="help-inline"', html, re.MULTILINE)
+ self.assertEqual(len(matches), 3)
+
+ form = TestForm({'email': 'invalidemail'})
+ form.helper = FormHelper()
+ form.helper.layout = layout
+ form.helper.error_text_inline = False
+ html = render_crispy_form(form)
+
+ matches = re.findall('<p id="error_\d_\w*" class="help-block"', html, re.MULTILINE)
+ self.assertEqual(len(matches), 3)
+
+ def test_error_and_help_inline(self):
+ form = TestForm({'email': 'invalidemail'})
+ form.helper = FormHelper()
+ form.helper.error_text_inline = False
+ form.helper.help_text_inline = True
+ form.helper.layout = Layout('email')
+ form.is_valid()
+ html = render_crispy_form(form)
+
+ # Check that help goes before error, otherwise CSS won't work
+ help_position = html.find('<span id="hint_id_email" class="help-inline">')
+ error_position = html.find('<p id="error_1_id_email" class="help-block">')
+ self.assertTrue(help_position < error_position)
+
+ # Viceversa
+ form = TestForm({'email': 'invalidemail'})
+ form.helper = FormHelper()
+ form.helper.error_text_inline = True
+ form.helper.help_text_inline = False
+ form.helper.layout = Layout('email')
+ form.is_valid()
+ html = render_crispy_form(form)
+
+ # Check that error goes before help, otherwise CSS won't work
+ error_position = html.find('<span id="error_1_id_email" class="help-inline">')
+ help_position = html.find('<p id="hint_id_email" class="help-block">')
+ self.assertTrue(error_position < help_position)
def test_attrs(self):
form = TestForm()
Please sign in to comment.
Something went wrong with that request. Please try again.