zopefoundation / z3c.form

An advanced form and widget framework for Zope 3
Other
8 stars 39 forks source link

SingleCheckboxWidget checked state not saved without ``value=selected`` #101

Open thet opened 3 years ago

thet commented 3 years ago

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:

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:

d-maurer commented 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.

icemac commented 3 years ago

@thet Did Dieter's explanation solve your problem, so the issue can be closed?