Skip to content

Commit

Permalink
Added InlineRadios bootstrap layout object
Browse files Browse the repository at this point in the history
This renders radio buttons inline. It's part of the fix for #106
  • Loading branch information
maraujop committed Nov 28, 2012
1 parent 2a61dff commit 35c1bbc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
16 changes: 16 additions & 0 deletions crispy_forms/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ def render(self, form, form_style, context, template_pack='bootstrap'):
return html


class InlineRadios(Field):
"""
Layout object for rendering radiobuttons inline::
InlineRadios('field_name')
"""
template = "bootstrap/layout/radioselect_inline.html"

def render(self, form, form_style, context, template_pack='bootstrap'):
context.update({'inline_class': 'inline'})
html = super(InlineRadios, self).render(form, form_style, context)
# We delete the inserted key to avoid side effects
del context.dicts[-2]['inline_class']
return html


class FieldWithButtons(Div):
template = 'bootstrap/layout/field_with_buttons.html'

Expand Down
2 changes: 1 addition & 1 deletion crispy_forms/templates/bootstrap/layout/radioselect.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% include 'bootstrap/layout/field_errors_block.html' %}

{% for choice in field.field.choices %}
<label class="radio">
<label class="radio {{ inline_class }}">
<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 }}
</label>
{% endfor %}
Expand Down
14 changes: 14 additions & 0 deletions crispy_forms/templates/bootstrap/layout/radioselect_inline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% if field.is_hidden %}
{{ field }}
{% else %}
<div id="div_{{ field.auto_id }}" class="control-group{% if form_show_errors and field.errors %} error{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}">

{% if field.label %}
<label for="{{ field.auto_id }}" class="control-label{% if field.field.required %} requiredField{% endif %}">
{{ field.label|safe }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
</label>
{% endif %}

{% include 'bootstrap/layout/radioselect.html' %}
</div>
{% endif %}
9 changes: 9 additions & 0 deletions crispy_forms/tests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ class CheckboxesTestForm(forms.Form):
widget = forms.CheckboxSelectMultiple,
)

inline_radios = forms.ChoiceField(
choices = (
('option_one', "Option one"),
('option_two', "Option two"),
),
widget = forms.RadioSelect,
initial = 'option_two',
)


class TestModel(models.Model):
email = models.CharField(max_length=20)
Expand Down
20 changes: 15 additions & 5 deletions crispy_forms/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
from crispy_forms.bootstrap import (
AppendedPrependedText, AppendedText, PrependedText, InlineCheckboxes,
FieldWithButtons, StrictButton
FieldWithButtons, StrictButton, InlineRadios
)
from crispy_forms.utils import render_crispy_form
from crispy_forms.templatetags.crispy_forms_tags import CrispyFormNode
Expand Down Expand Up @@ -599,19 +599,21 @@ def test_context_pollution(self):

def test_hidden_fields(self):
form = TestForm()
# All fields fake hidden
# All fields hidden
for field in form.fields:
form[field].field.widget.is_hidden = True
form.fields[field].widget = forms.HiddenInput()

form.helper = FormHelper()
form.helper.layout = Layout(
AppendedText('password1', 'foo'),
PrependedText('password2', 'bar'),
AppendedPrependedText('email', 'bar'),
InlineCheckboxes('first_name'),
InlineRadios('last_name'),
)
html = render_crispy_form(form)
self.assertEqual(html.count("<input"), 4)
self.assertEqual(html.count("<input"), 5)
self.assertEqual(html.count('type="hidden"'), 5)

def test_field_with_buttons(self):
form = TestForm()
Expand Down Expand Up @@ -967,7 +969,7 @@ def test_multiplecheckboxes(self):
test_form = CheckboxesTestForm()
html = render_crispy_form(test_form)

self.assertEqual(html.count('checked="checked"'), 5)
self.assertEqual(html.count('checked="checked"'), 6)

test_form.helper = FormHelper(test_form)
test_form.helper[1].wrap(InlineCheckboxes, inline=True)
Expand Down Expand Up @@ -1027,6 +1029,14 @@ def test_appended_prepended_text(self):
self.assertEqual(html.count('<span class="add-on">#</span>'), 1)
self.assertEqual(html.count('<span class="add-on">$</span>'), 1)

def test_inline_radios(self):
test_form = CheckboxesTestForm()
test_form.helper = FormHelper()
test_form.helper.layout = Layout(InlineRadios('inline_radios'))
html = render_crispy_form(test_form)

self.assertEqual(html.count('radio inline"'), 2)


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

0 comments on commit 35c1bbc

Please sign in to comment.