web/formfields.py
branchstable
changeset 6114 3e1718a2db55
parent 5955 67dfe437bf25
child 6139 f76599a96238
child 6218 d37428222a63
equal deleted inserted replaced
6113:dbe17a3ebb9f 6114:3e1718a2db55
   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