Open thet opened 3 years ago
Johannes Raggam wrote at 2021-3-22 17:13 +0000:
... I suggest to also add the term
on
to https://github.com/zopefoundation/z3c.form/blob/440eb18c541b88f64e88be9b66fbc27748f75af2/src/z3c/form/browser/checkbox.py#L90 Not sure, but I guess that would solve the issue...
This likely will not work:
As the docstring for SequenceWidget
tells us its purpose is to
support the use case "possible input values specified via a sequence
of values".
You suggest to use a two element sequence as values for a
SingleCheckboxWidget
but such a widget supports only a single value.
You might avoid HTML problems by the wrong SequenceWidget
use
through your manually created HTML but you must keep in mind
that there will be a value extractor which translates the widget
value into the field value and it likely does not know how to
handle a new value "on".
The z3c.form
framework is quite complex being composed of
a set of cooperating components, among them
"field", "widget", "input template", "value extractor".
Each of them makes assumptions about the cooperating partners.
Therefore, it is best for customization purposes to start
with the default component and adapt it as necessary
instead of developing a component from ground zero.
When you look at checkbox_input.pt
(the input template component
for a checkbox), you will see that the widget instance is supposed
to provide the value to be used in the HTML
(this way, the same widget can be used for checkboxes as well
as for radio buttons). I suggest that your customized template does
the same.
@thet Did Dieter's explanation solve your problem, so the issue can be closed?
What I did:
I had a field which uses a z3c.form
SingleCheckboxWidget
. Field definition:And this was the manually set HTML code for the 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#valueThe
extract
method of theSequenceWidget
did return the default value, because the lookup forself.terms.getTermsByToken(term)
failed withterm
beingon
: https://github.com/zopefoundation/z3c.form/blob/440eb18c541b88f64e88be9b66fbc27748f75af2/src/z3c/form/widget.py#L255The terms for the
SingleCheckboxWidget
are set here: https://github.com/zopefoundation/z3c.form/blob/440eb18c541b88f64e88be9b66fbc27748f75af2/src/z3c/form/browser/checkbox.py#L90Using the following input with a value set to "selected" did solve the issue:
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 https://github.com/zopefoundation/z3c.form/blob/440eb18c541b88f64e88be9b66fbc27748f75af2/src/z3c/form/browser/checkbox.py#L90 Not sure, but I guess that would solve the issue...What version of Python and Zope/Addons I am using: