Labels |
Added:
?
|
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2018-10-10 14:13:15 |
Closed_By | ⇒ | Quy |
Closed_By | Quy | ⇒ | joomla-cms-bot |
Set to "closed" on behalf of @Quy by The JTracker Application at issues.joomla.org/joomla-cms/22575
@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.
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">
.
But then why not just set value="myValue"
?
@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.
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" />
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.
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?
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.
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.
default
attribute here defines the value of generated input element. Usechecked="true"
to indicate that the checkbox should be checked by default.