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