858 :class:`~cubicweb.web.formwidgets.Radio` with yes/no values. You |
858 :class:`~cubicweb.web.formwidgets.Radio` with yes/no values. You |
859 can change that values by specifing `choices`. |
859 can change that values by specifing `choices`. |
860 """ |
860 """ |
861 widget = fw.Radio |
861 widget = fw.Radio |
862 |
862 |
|
863 def __init__(self, allow_none=False, **kwargs): |
|
864 super(BooleanField, self).__init__(**kwargs) |
|
865 self.allow_none = allow_none |
|
866 |
863 def vocabulary(self, form): |
867 def vocabulary(self, form): |
864 if self.choices: |
868 if self.choices: |
865 return super(BooleanField, self).vocabulary(form) |
869 return super(BooleanField, self).vocabulary(form) |
|
870 if self.allow_none: |
|
871 return [('', ''), (form._cw._('yes'), '1'), (form._cw._('no'), '0')] |
|
872 # XXX empty string for 'no' in that case for bw compat |
866 return [(form._cw._('yes'), '1'), (form._cw._('no'), '')] |
873 return [(form._cw._('yes'), '1'), (form._cw._('no'), '')] |
867 |
874 |
868 def _ensure_correctly_typed(self, form, value): |
875 def _ensure_correctly_typed(self, form, value): |
|
876 if self.allow_none: |
|
877 if value: |
|
878 return bool(int(value)) |
|
879 return None |
869 return bool(value) |
880 return bool(value) |
870 |
881 |
871 |
882 |
872 class FloatField(IntField): |
883 class FloatField(IntField): |
873 """Use this field to edit floats (`Float` yams type). This field additionaly |
884 """Use this field to edit floats (`Float` yams type). This field additionaly |