[formwidgets] new allow_none attribute on BooleanField allowing a third None value for booleans stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 17 Aug 2010 15:22:57 +0200
branchstable
changeset 6114 3e1718a2db55
parent 6113 dbe17a3ebb9f
child 6115 56ec278b843e
[formwidgets] new allow_none attribute on BooleanField allowing a third None value for booleans
web/formfields.py
--- a/web/formfields.py	Tue Aug 17 15:22:04 2010 +0200
+++ b/web/formfields.py	Tue Aug 17 15:22:57 2010 +0200
@@ -854,12 +854,23 @@
     """
     widget = fw.Radio
 
+    def __init__(self, allow_none=False, **kwargs):
+        super(BooleanField, self).__init__(**kwargs)
+        self.allow_none = allow_none
+
     def vocabulary(self, form):
         if self.choices:
             return super(BooleanField, self).vocabulary(form)
+        if self.allow_none:
+            return [('', ''), (form._cw._('yes'), '1'), (form._cw._('no'), '0')]
+        # XXX empty string for 'no' in that case for bw compat
         return [(form._cw._('yes'), '1'), (form._cw._('no'), '')]
 
     def _ensure_correctly_typed(self, form, value):
+        if self.allow_none:
+            if value:
+                return bool(int(value))
+            return None
         return bool(value)