Skip to content

Commit

Permalink
Django widget attrs not applied to RadioSelect and CheckboxSelectMult…
Browse files Browse the repository at this point in the history
…iple

Fixes #206
  • Loading branch information
maraujop committed Jul 27, 2013
1 parent beb32ac commit e08a97c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.3.3

* If Django widget attrs where set for `RadioSelect` or `CheckboxSelectMultiple` they were not being rendered by crispy-forms, see #206.
* `form_show_labels` wasn't working correctly with some layout objects, see #193.

## 1.3.2 (2013/6/23)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% load crispy_forms_filters %}

<div class="controls"{% if flat_attrs %} {{ flat_attrs|safe }}{% endif %}>
{% include 'bootstrap/layout/field_errors_block.html' %}

{% for choice in field.field.choices %}
<label class="checkbox{% if inline_class %} {{ inline_class }}{% endif %}">
<input type="checkbox"{% if choice.0 in field.value or choice.0|stringformat:"s" in field.value or choice.0|stringformat:"s" == field.value|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0 }}">{{ choice.1 }}
<input type="checkbox"{% if choice.0 in field.value or choice.0|stringformat:"s" in field.value or choice.0|stringformat:"s" == field.value|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0 }}" {{ field.field.widget.attrs|flatatt }}>{{ choice.1 }}
</label>
{% endfor %}

Expand Down
4 changes: 3 additions & 1 deletion crispy_forms/templates/bootstrap/layout/radioselect.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% load crispy_forms_filters %}

<div class="controls"{% if flat_attrs %} {{ flat_attrs|safe }}{% endif %}>
{% include 'bootstrap/layout/field_errors_block.html' %}

{% for choice in field.field.choices %}
<label class="radio{% if inline_class %} {{ inline_class }}{% endif %}">
<input type="radio"{% if choice.0|stringformat:"s" == field.value|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0 }}">{{ choice.1 }}
<input type="radio"{% if choice.0|stringformat:"s" == field.value|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0 }}" {{ field.field.widget.attrs|flatatt }}>{{ choice.1 }}
</label>
{% endfor %}

Expand Down
7 changes: 7 additions & 0 deletions crispy_forms/templatetags/crispy_forms_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from django.template import Context
from django.template.loader import get_template
from django.utils.functional import memoize
from django.utils.safestring import mark_safe
from django import template

from crispy_forms.exceptions import CrispyError
from crispy_forms.utils import flatatt

TEMPLATE_PACK = getattr(settings, 'CRISPY_TEMPLATE_PACK', 'bootstrap')
DEBUG = getattr(settings, 'DEBUG', False)
Expand Down Expand Up @@ -84,3 +86,8 @@ def as_crispy_field(field, template_pack=TEMPLATE_PACK):
template = get_template('%s/field.html' % template_pack)
c = Context({'field': field, 'form_show_errors': True})
return template.render(c)


@register.filter(name='flatatt')
def flatatt_filter(attrs):
return mark_safe(flatatt(attrs))
9 changes: 9 additions & 0 deletions crispy_forms/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,15 @@ def test_html_with_carriage_returns(self):
else:
self.assertEqual(html.count('\n'), 24)

def test_radio_attrs(self):
if self.current_template_pack == 'bootstrap':
form = CheckboxesTestForm()
form.fields['inline_radios'].widget.attrs = {'class': "first"}
form.fields['checkboxes'].widget.attrs = {'class': "second"}
html = render_crispy_form(form)
self.assertTrue('class="first"' in html)
self.assertTrue('class="second"' in html)


class TestDynamicLayouts(CrispyTestCase):
def setUp(self):
Expand Down

0 comments on commit e08a97c

Please sign in to comment.