1. Carl Meyer
  2. django-form-utils

Commits

Carl Meyer  committed 20bdff3

Add is_radio filter.

  • Participants
  • Parent commits 4a2f4a0
  • Branches default

Comments (0)

Files changed (2)

File form_utils/templatetags/form_utils.py

View file
  • Ignore whitespace
     return isinstance(boundfield.field, forms.MultipleChoiceField)
 
 
+
 @register.filter
 def is_select(boundfield):
     """Return True if this field is a ChoiceField (or subclass)."""
     return isinstance(boundfield.field, forms.ChoiceField)
+
+
+
+@register.filter
+def is_radio(boundfield):
+    """
+    Return True if this field's widget's class name contains 'radio'.
+
+    This hacky approach is necessary in order to support django-floppyforms,
+    whose RadioSelect does not inherit from Django's built-in RadioSelect.
+
+    """
+    return 'radio' in boundfield.field.widget.__class__.__name__.lower()

File tests/tests.py

View file
  • Ignore whitespace
                 choices=(("b", "Beginner"), ("a", "Advanced")), required=False)
             colors = forms.MultipleChoiceField(
                 choices=[("red", "red"), ("blue", "blue")])
+            gender = forms.ChoiceField(
+                choices=(("m", "Male"), ("f", "Female"), ("o", "Other")),
+                widget=forms.RadioSelect(),
+                required=False,
+                )
             awesome = forms.BooleanField(required=False)
 
         return PersonForm
         f = self.form()
 
         self.assertFalse(self.form_utils.is_select(f["name"]))
+
+
+    def test_is_radio(self):
+        """`is_radio` detects a radio select widget."""
+        f = self.form()
+
+        self.assertTrue(self.form_utils.is_radio(f["gender"]))
+
+
+    def test_is_not_radio(self):
+        """`is_radio` detects a non-radio select."""
+        f = self.form()
+
+        self.assertFalse(self.form_utils.is_radio(f["level"]))