Skip to content

Commit

Permalink
limit renderer to RadioSelect; added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxcanfly committed Mar 21, 2014
1 parent 45150e6 commit 491e434
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion parsley/decorators.py
Expand Up @@ -23,7 +23,7 @@
def update_widget_attrs(field, prefix='data'):
attrs = field.widget.attrs
if field.required:
if isinstance(field, forms.ChoiceField):
if isinstance(field.widget, forms.widgets.RadioSelect):
# Use a mixin, to try and support non-standard renderers if possible
class ParsleyChoiceFieldRenderer(ParsleyChoiceFieldRendererMixin, field.widget.renderer):
parsley_namespace = prefix
Expand Down
11 changes: 10 additions & 1 deletion parsley/tests/forms.py
Expand Up @@ -101,12 +101,21 @@ def get_state_choices():

@parsleyfy
class FormWithCustomChoices(forms.Form):
state = forms.ChoiceField(widget = forms.Select(choices=[]))
state = forms.ChoiceField(widget=forms.Select(choices=[]))
def __init__(self, *args, **kwargs):
super(FormWithCustomChoices, self).__init__(*args, **kwargs)
self.fields['state'] = forms.ChoiceField(
choices=get_state_choices())

@parsleyfy
class FormWithRadioSelect(forms.Form):
state = forms.ChoiceField(choices=get_state_choices(), widget=forms.RadioSelect)


@parsleyfy
class FormWithRadioSelectNotRequired(forms.Form):
state = forms.ChoiceField(choices=get_state_choices(), required=False, widget=forms.RadioSelect)


@parsleyfy
class FormWithMedia(forms.Form):
Expand Down
18 changes: 17 additions & 1 deletion parsley/tests/tests.py
Expand Up @@ -12,7 +12,7 @@
ExtraDataMissingFieldForm, FormWithWidgets, StudentModelForm,
FormWithCleanField, FormWithCustomInit, FormWithCustomChoices,
FormWithMedia, FormWithoutMedia, MultiWidgetForm, CustomErrorMessageForm,
CustomPrefixForm)
CustomPrefixForm, FormWithRadioSelect, FormWithRadioSelectNotRequired)
from .models import Student
from .admin import StudentAdmin

Expand Down Expand Up @@ -157,6 +157,22 @@ def test_custom_choices(self):
[("NY", "NY"), ("OH", "OH")])


class TestRadioSelect(ParsleyTestCase):
def test_radio_select(self):
form = FormWithRadioSelect()
self.assertEqual(form.fields['state'].choices,
[("NY", "NY"), ("OH", "OH")])
radio_select_html = form.fields['state'].widget.render("state", "NY")
self.assertEqual(1, len(re.findall('data-parsley-mincheck', radio_select_html)))

def test_radio_select_not_required(self):
form = FormWithRadioSelectNotRequired()
self.assertEqual(form.fields['state'].choices,
[("NY", "NY"), ("OH", "OH")])
radio_select_html = form.fields['state'].widget.render("state", "NY")
self.assertEqual(0, len(re.findall('data-parsley-mincheck', radio_select_html)))


class TestCleanFields(ParsleyTestCase):
def test_clean(self):
form = FormWithCleanField(data={"description": "foo"})
Expand Down
2 changes: 1 addition & 1 deletion parsley/widgets.py
Expand Up @@ -4,7 +4,7 @@ def __iter__(self):
yield self[i]

def __getitem__(self, idx):
choice = self.choices[idx]
choice = self.choices[idx]
attrs = self.attrs.copy()
if idx == len(self.choices) - 1:
attrs["{prefix}-mincheck".format(prefix=self.parsley_namespace)] = "1"
Expand Down

0 comments on commit 491e434

Please sign in to comment.