?
avatar JamilaCisse
JamilaCisse
10 Oct 2018

Steps to reproduce the issue

  1. Add a checkbox field to a component
  2. Set the default value to 1
    screenshot from 2018-10-10 13-29-58

Expected result

When opening the form, the checkbox is checked

Actual result

When opening the form, the checkbox is unchecked
screenshot from 2018-10-10 13-27-19

System information (as much as possible)

Joomla! 3.8.13

Additional comments

avatar JamilaCisse JamilaCisse - open - 10 Oct 2018
avatar joomla-cms-bot joomla-cms-bot - change - 10 Oct 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 10 Oct 2018
avatar SharkyKZ
SharkyKZ - comment - 10 Oct 2018

default attribute here defines the value of generated input element. Use checked="true" to indicate that the checkbox should be checked by default.

avatar Quy Quy - change - 10 Oct 2018
Status New Closed
Closed_Date 0000-00-00 00:00:00 2018-10-10 14:13:15
Closed_By Quy
avatar joomla-cms-bot joomla-cms-bot - change - 10 Oct 2018
Closed_By Quy joomla-cms-bot
avatar joomla-cms-bot joomla-cms-bot - close - 10 Oct 2018
avatar joomla-cms-bot
joomla-cms-bot - comment - 10 Oct 2018

Set to "closed" on behalf of @Quy by The JTracker Application at issues.joomla.org/joomla-cms/22575

avatar aDaneInSpain
aDaneInSpain - comment - 11 Oct 2018

@SharkyKZ thanks for replying. Unfortunately the Joomla documentation is not reflecting this feature but I have submitted an issue to them.

Would you mind explaining to me what the default value is used for. I don't really understand it.

avatar SharkyKZ
SharkyKZ - comment - 11 Oct 2018

It sets the value of the field. A checkbox can have any value, not just 1. For example, checkbox field with default="myValue" will generate <input type="checkbox" value="myValue">.

avatar aDaneInSpain
aDaneInSpain - comment - 11 Oct 2018

But then why not just set value="myValue"?

avatar aDaneInSpain
aDaneInSpain - comment - 11 Oct 2018

@brianteeman That just shows how to (in HTML) set a checkbox to be checked, I don't really see how that is relevant here as we are discussing how to get Joomla to write that code, not how to write that code ourselves.

My main question now is what is the difference between setting value="1" and default="1" in the XML definition of a field.

avatar aDaneInSpain
aDaneInSpain - comment - 11 Oct 2018

In fact from the documentation:

What would be the difference between

<field name="show_title" type="checkbox" label="Show title" description="Show the title of the item" value="1" default="0" />

and

<field name="show_title" type="checkbox" label="Show title" description="Show the title of the item" value="1" default="1" />

avatar brianteeman
brianteeman - comment - 11 Oct 2018

It is relevant because the way to have a checkbox default to being checked is to give it an attribute of checked

You can see this in the core of joomla here

The attribute checked is not the same thing as a value. It is all in the link I posted.

avatar aDaneInSpain
aDaneInSpain - comment - 11 Oct 2018

I am fully aware of how a checkbox is checked in the browser but that is not what I am now asking.

Now I know that I should use checked="true" or I guess like your example checked="1" to check the checkbox inside Joomla's XML file to get Joomla to write checked inside the HTML input tag (even though this was omitted from the documentation).

What I do not understand is what is difference between default and value? To me it makes no sense for a checkbox to have a default attribute, In fact the specifications does not have a default attribute so why do we in Joomla and what does it do?

avatar mbabker
mbabker - comment - 11 Oct 2018

The Joomla form XML schema is not a one-for-one match with the HTML spec on the HTML input types, remember that the XML schema is a definition of the form and that it is parsed to create the HTML representation (in many cases the attributes you specify in the XML and what actually renders in HTML do align but there are several attributes that only apply in the context of our XML schema, including translate_label, translate_description, labelclass, and showon).

default is an attribute available to all form fields (as it is defined as part of the base Joomla\CMS\Form\FormField class) which specifies a default value for a field if when the form is submitted the field is empty; this is applied while the form data is passed through Joomla\CMS\Form\Form::filter().

You're right that in the context of a checkbox field it doesn't really make sense. Ignore it if you choose, but it'd be pretty painful to refactor the form field classes to remove that particular attribute from the checkbox field type.

avatar aDaneInSpain
aDaneInSpain - comment - 11 Oct 2018

I figured this was most likely the answer. So in my opinion it should just be ignored, which is what I will ask the documentation team to do as it will otherwise be confusing as people may think that setting the default to the same value as value will check the checkbox.

Add a Comment

Login with GitHub to post a comment