Checkbox type plugin parameter with default="0" is unsaveable
Make a plugin with the following parameter in the manifest:
<extension version="3.0" type="plugin" group="content" method="upgrade">
<!-- ... -->
<config>
<fields name="params">
<fieldset name="Parameters" label="Parameters">
<field name="MyCheckbox" type="checkbox" label="My Checkbox" default="0"/>
</fieldset>
</fields>
</config>
</extension>
The parameter is now not settable to checked. If you check it in the plugin's options page and save, it's 0 in the database and unchecked if you visit the plugin options again.
Hard to tell when did it break. As of Joomla 5.4.0, the default causes the <input type="checkbox"> element to have a value="0", which is wrong - when the form is submitted, the zero goes into the checkbox' name-value pair in the URL encoded form, i. e. MyCheckbox=0, so that 0 ends up being the parameter value as far as the back-end is concerned.
@sevaa According to the documentation, the checkbox field does not have a "default" property: https://manual.joomla.org/docs/general-concepts/forms-fields/standard-fields/checkbox/
You should use the "checked" property instead:
checked (optional) should be set to 1 to check the checkbox by default or 0 if not.
Could you check and report back if that works for you?
That is expected behavior, kind of. With default value you will be unable to save unchecked checkbox, because the default value will be taken.
I would suggest to use Radio or Select instead.
All I can say, it used to work. Also, I didn't come up with default by myself - it was listed in a manifest reference at some point.
hm, okay last change in this field was https://github.com/joomla/joomla-cms/pull/37174 not really see how it may affect
All I can say, it used to work. Also, I didn't come up with
defaultby myself - it was listed in a manifest reference at some point.
@sevaa Possibly I was wrong and it is just not well documented in the new developer docs.
hm, okay last change in this field was #37174
That was released with Joomla 5.2.4 in February.
@sevaa Could you approximately say when it has stopped to work for you?
Works as expected in 4.4.14. I jumped from that straight to what was under Joomla Next (5.4), so impossible to tell what it was like in earlier 5.x releases.
Notably, the <input> element on the plugin options form in J4.4.14 has value="1" in it. Not the zero from the default.
The issue indeed causes by PR https://github.com/joomla/joomla-cms/pull/37174.
@sevaa In your case, I guess you can change default="" to have it works (or just remove default attribute from the field definition). At this time, I'm afraid of we could not do anything:
- Revert original change causes backward incompatible break
- Fix the checked state does not really solve the issue because I guess you would want if the checkbox is checked, the value for the parameter store Truthy value but it is not (it stores '0' in this case and if you use that parameter in an if clause, it is still false)
The way I see it, there are two problems:
- despite the
checkboxXML element not having a documenteddefaultattribute, it's taken into account anyway - setting or checking the value of a checkbox control to set or learn its checked/unckecked state is wrong The latter might be driven by an understandable but misguided desire to treat all HTML form elements uniformly in code.