SingleCheckboxWidget checked state not saved without ``value=selected``
thet opened this issue · comments
What I did:
I had a field which uses a z3c.form SingleCheckboxWidget
. Field definition:
some_bool = zope.schema.Bool(
title="some title",
required=False,
default=False,
)
And this was the manually set HTML code for the checkbox:
<input name="some_bool" type="checkbox" />
What I expect to happen:
Saving the form with a checked checkbox should have resulted in a set boolean for the object.
What actually happened:
The boolean was not set because:
-
The default checked value for a input field without a
value
attribute ison
. See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#value -
The
extract
method of theSequenceWidget
did return the default value, because the lookup forself.terms.getTermsByToken(term)
failed withterm
beingon
:
z3c.form/src/z3c/form/widget.py
Line 255 in 440eb18
-
The terms for the
SingleCheckboxWidget
are set here:z3c.form/src/z3c/form/browser/checkbox.py
Line 90 in 440eb18
-
Using the following input with a value set to "selected" did solve the issue:
<input name="some_bool" type="checkbox" value="selected" />
I'd have expected that a <input name="some_bool" type="checkbox"/>
as a prototypical representation of booleans in html forms to just work out of the box.
I suggest to also add the term on
to
z3c.form/src/z3c/form/browser/checkbox.py
Line 90 in 440eb18
Not sure, but I guess that would solve the issue...
What version of Python and Zope/Addons I am using:
- Ubuntu 20.04.1
- Python 3.8.5
- z3c.form-3.7.1-py3.8.egg
- Zope-4.5.5-py3.8.egg
- Products.CMFPlone-5.2.4-py3.8.egg
@thet Did Dieter's explanation solve your problem, so the issue can be closed?