5

My component needs to save mulitple options, and I can't seem to get my head wrapped around how to do it. Referring to Checkboxes form field type, I'm not sure how Joomla is expecting to save the options that the user has selected.

Update: To be clear, i'm referring to the plural checkboxes field type, not checkbox.

Here's the XML that I'm using:

<field name="content_subrating"
    label="Content Subrating"
    type="checkboxes"
    >
    <option value="D">Suggestive Dialog</option>
    <option value="L">Course Language</option>
    <option value="S">Sexual Content</option>
    <option value="V">Violence</option>
</field>

Resulting HTML

<fieldset class="checkboxes" id="jform_content_subrating">
    <ul>
        <li>
            <input type="checkbox" checked="" value="D" name="jform[content_subrating][]" id="jform_content_subrating0">
            <label for="jform_content_subrating0" aria-invalid="false">Suggestive Dialog</label>
        </li>
        <li>
            <input type="checkbox" value="L" name="jform[content_subrating][]" id="jform_content_subrating1">
            <label for="jform_content_subrating1">Course Language</label>
        </li>
        <li>
            <input type="checkbox" value="S" name="jform[content_subrating][]" id="jform_content_subrating2">
            <label for="jform_content_subrating2">Sexual Content</label>
        </li>
        <li>
            <input type="checkbox" value="V" name="jform[content_subrating][]" id="jform_content_subrating3">
            <label for="jform_content_subrating3">Violence</label>
        </li>
    </ul>
</fieldset>

And the resulting input:

JRequest::getVar('jform')['content_subrating']) =>
Array
(
    [0] => L
    [1] => V
)

In my perfect world, it would concatenate all the value and store them in one column, but I don't expect that's what Joomla would do by default. If not, then how would one specify which values go to where such that Joomla can later understand how to retrieve the values. I'm sure I'm missing something obvious, but I'm stuck.

Can someone explain how Joomla deals with this, or give me a core example that does?

Conclusion: It seems that this field type is merely a short hand method of easily creating multiple checkboxes, all of which have the inherent problem of submitting nothing if unchecked. Because they're submitted as an array, it seems handy enough as long as you're merely processing based on the values being submitted, but seems like a lot of unnecessary work if you ever need to retrieve them. (Just my opinion, I could be wrong)

Further Realization (at the very top of the documentation!):

The checkboxes form field type provides a set of checkboxes. Note: unlike most standard form field types, such as textfield or checkbox, this field is not an "out of the box" solution. It will create checkboxes for you, and submit their values in form of an array, but it will not store them in the database.

0

4 Answers 4

4

My first offering, so please don't shoot me. I've been fighting this one for a while and FINALLY found a solution that works well. Hoping to help others avoid this nightmare.

The key piece I was missing is that the jform was submitting the checkboxes field as an array (duh) so accessing jform['whatever'] did not give the expected results.

The OP said:

In my perfect world, it would concatenate all the value and store them in one column

YES! You can do this with checkboxes. It works great.

Here's what I did.

In the admin/models/forms/myform.xml I have a checkboxes field:

<field
        type="checkboxes"
        label="myfield"
        name="myfield"
        default="none"
        multiple="true">
        <option value="something">something</option>
...
        <option value="something else">something else</option>
</field>

In the admin/views/myview/edit.php form field, I just use:

<?php echo $field->label; echo $field->input;?>

In the admin/models/myview.php I added a function:

protected function prepareTable($table)
{
    // Fix the checkboxes data
    $form = JFactory::getApplication()->input->get('jform', array(), 'array');
    $myfield = $form['myfield'][0];

    if (!isset($myfield))
    {
        $myfield = "";
        $table->myfield= $myfield;
    } 
    else 
    {
        $checkbox_options = $table->myfield;
        $table->myfield = implode(",", $checkbox_options);
    }
}

It stores the data as comma separated values in the column. It stores a blank if the checkboxes are un-checked. YEAH!

Since my form is also edited on the front end, I did almost the same thing in the front end. Form field and form are the same.

Since I was already over-riding the save function in the controller, I just added this (with my other code as well):

public function save() 
{
    $data = JFactory::getApplication()->input->get('jform', array(), 'array');
    $myfield = $data['myfield'][0];

    if (!isset($myfield))
    {
        $myfield = "";
        $data['myfield'] = $myfield;
    } 
    else 
    {
        $checkbox_options = $data['myfield'];
        $data['myfield'] = implode(",", $checkbox_options);
    }
}

I'm sure I could have standardized that code even more. But right now I'm just happy to have this working.

Relevant documentation: https://docs.joomla.org/Checkboxes_form_field_type

0
3

Seems to me you want to save all the results as an array, and then save that array as JSON in a single field.

Example of field.. look at your database, #__content table, see the attribs field for example of how joomla stores multiple pieces of info in one field as JSON

Then just look at how the com_content models and controller work for some reverse engineering goodness!

1
  • Even though this is referring to "checkbox", not "checkboxes", and I may wind up using the answer from @Anibal, this is the closest thing to answering the ultimate question - how core Joomla handles multiple submitted values for the same field - regardless of the field type that submitted them.
    – GDP
    Jul 22, 2014 at 13:40
2

It's less what Joomla does as much more what HTML does. Checkboxes are only sent when checked. Those which aren't checked aren't sent. It's like they don't exist.

So the result you get is actually according to HTML standards.

Personally, I avoid using checkboxes for options as much as possible. They just act to strange to be taken seriously :-)

1
  • Well, I'm inclined to agree, but one cannot ignore checkboxes, thus my interest in the plural checkboxes, and how it might differ from checkbox.
    – GDP
    Jul 22, 2014 at 13:33
1

In Users / Mass Mail (administrator/components/com_users/models/forms/mail.xml), you can check an example of its usage. Each checkbox is defined in its own field, instead for managing an array of values:

<field name="recurse" type="checkbox"
    description="COM_USERS_MAIL_FIELD_RECURSE_DESC"
    label="COM_USERS_MAIL_FIELD_RECURSE_LABEL"
    value="1"
/>

<field name="mode" type="checkbox"
    description="COM_USERS_MAIL_FIELD_SEND_IN_HTML_MODE_DESC"
    label="COM_USERS_MAIL_FIELD_SEND_IN_HTML_MODE_LABEL"
    value="1"
/>

<field name="disabled" type="checkbox"
    description="COM_USERS_MAIL_FIELD_EMAIL_DISABLED_USERS_DESC"
    label="COM_USERS_MAIL_FIELD_EMAIL_DISABLED_USERS_LABEL"
    value="1"
/>
3
  • This also is checkbox, not checkboxes, but may ultimately be the answer to my problem, not necessarily the question, lol
    – GDP
    Jul 22, 2014 at 13:32
  • Yes, I think this model is simpler than the array of values representation.
    – Anibal
    Jul 22, 2014 at 19:39
  • Which is why I'm considering this in my solution. I asked the question mostly to understand how checkboxes is supposed to work in Joomla
    – GDP
    Jul 23, 2014 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.