C5-8.3: How to save names and values of checked checkboxes in config DB?

Permalink
Hello.

In an Add Block form I have a group of checkboxes.

The checkboxes are shown in the form as
echo $form->checkbox('currency[]', $currency, true);

The form HTML becomes:
<input id="currency_USD" name="currency[]" class="ccm-input-checkbox" value="USD" checked="checked" type="checkbox">

The db.xml has this:
<field name="currency" type="array"/>

Could anyone please tell me how I can store and then get names of all checked boxes in the block's DB? What do I put in the controller's save() function?

At the moment a string 'Array' gets stored in the DB's array.

linuxoid
 
pvernaglia replied on at Permalink Reply
pvernaglia
I serialize an array in the controller save() function to save to the db and use a type x2

$args['currency'] = serialize($args['currency']);
linuxoid replied on at Permalink Reply
linuxoid
I tried that, but then it needs to be deserialized which, for some reason, still returns an 'Array' string instead of the array.
MrKDilkington replied on at Permalink Reply
MrKDilkington
The core Calendar block has an example of saving a group of checkboxes as an array.

- each checkbox shares the same input name "lightboxProperties[]"
- controller save() method
$lightboxProperties = array();
if (isset($args['lightboxProperties']) && is_array($args['lightboxProperties'])) {
    $lightboxProperties = $args['lightboxProperties'];
}
$args['lightboxProperties'] = json_encode($lightboxProperties);

- controller edit() and add() method
$lightboxProperties = array(
    'title' => t('Title'),
    'date' => t('Date'),
    'description' => t('Description'),
    'linkToPage' => t('Link to Page'),
);

- controller add() method
$this->set('lightboxPropertiesSelected', array());

- controller edit() method
$this->set('lightboxPropertiesSelected', $this->getSelectedLightboxProperties());

- controller custom method for JSON decoding
public function getSelectedLightboxProperties()
{
    return (array) json_decode($this->lightboxProperties);
}

- form
<?php foreach ($lightboxProperties as $key => $name) { ?>
    <div class="checkbox"><label>
            <?=$form->checkbox('lightboxProperties[]', $key, in_array($key, $lightboxPropertiesSelected))?>
            <?=$name?>
        </label>
    </div>
<?php } ?>

- form output
<div>
    <div class="checkbox">
        <label><input type="checkbox" id="lightboxProperties_title" name="lightboxProperties[]" class="ccm-input-checkbox" value="title" checked="checked"> Title </label>
    </div>
    <div class="checkbox">
        <label><input type="checkbox" id="lightboxProperties_date" name="lightboxProperties[]" class="ccm-input-checkbox" value="date" checked="checked"> Date </label>
    </div>
    <div class="checkbox">
        <label><input type="checkbox" id="lightboxProperties_description" name="lightboxProperties[]" class="ccm-input-checkbox" value="description" checked="checked"> Description </label>
    </div>
    <div class="checkbox">
        <label><input type="checkbox" id="lightboxProperties_linkToPage" name="lightboxProperties[]" class="ccm-input-checkbox" value="linkToPage" checked="checked"> Link to Page </label>
    </div>
    <div class="checkbox">
        <label><input type="checkbox" id="lightboxProperties_ak_26" name="lightboxProperties[]" class="ccm-input-checkbox" value="ak_26" checked="checked"> Event Categories </label>

- $_POST array for lightboxProperties if all 5 checkboxes are checked
Example:
array( 
    0 => 'title',
    1 => 'date',
    2 => 'description',
    3 => 'linkToPage',
    4 => 'ak_26',
)

- $_POST array for lightboxProperties if only the "Title" checkbox is checked
- the input value for "Title" is "title"
Example:
array( 
    0 => 'title'
)

You can JSON encode/serialize an array before saving, then JSON decode/deseriialize it when retrieving it from the database.