views:

48

answers:

2

I've got a boolean field,

is_operable = BooleanField(widget=RadioSelect(choices=YES_OR_NO, renderer=InlineRadioFieldRenderer), required=False, initial=True)

Which is rendered as two radio buttons (yes or no), but I want it to be required. The problem is that if I change it to required=True, it throws a validation error when it gets False.

Is there a way around this?


YES_OR_NO = (
    (True, 'Yes'),
    (False, 'No')
)
+1  A: 

Leave it as required=False and write a custom clean_is_operable method that checks that you've got what you want, else throws a ValidationError or the like

stevejalim
+1  A: 

I would recommend using a TypedChoiceField which coerces the choices YES_OR_NO into a boolean. See the docs: http://docs.djangoproject.com/en/1.2/ref/forms/fields/#django.forms.TypedChoiceField

Mark Lavin
Yessir, that's an easy fix :) `TypedChoiceField(widget=RadioSelect, choices=YES_OR_NO, required=True, initial=True, empty_value=None, coerce=bool)`
Mark