If the form contains a checkbox and the user leaves it unchecked, I would
say the correct value to store is False. Storing None would make sense if
the boolean field were completely excluded from the form. If an empty
checkbox should be recorded as None, then when would you ever record False
-- and if the answer is never, then why not just use False rather than None
to distinguish between True and not True?
Anyway, if you want an unchecked box to be stored as None, you can do:
Field("mybool", "boolean", required = False, notnull = False, default =
None,
filter_in=lambda v: None if v is False else v)
Anthony
On Thursday, July 7, 2016 at 3:32:08 PM UTC-4, Alfonso Serra wrote:
>
> If i define a boolean field as:
>
> Field("mybool", "boolean", required = False, notnull = False, default =
> None)
>
> Its stored as False if its not present in request.post_vars. The proper
> behaviour would be to store a null value in this case, which allows to
> handle boolean's tri-states.
>
> To achieve bool tri states i have to hack the form after is processed like:
> if request.post_vars.mybool == None:
> form.vars.mybool = None
>
> Is there an easier/cleaner way to achieve this?
>
> Thanks.
>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.