Saving custom checkbox attribute with multiple values

I've got what I thought was a fairly simple task, but I'm obviously missing something because I can't get it to work. I was hoping you smarties could take a look.

I'm trying to save the values from a page custom attribute of type multiple select (checkboxes), but instead of saving all the values, I only get the value of the last item. Here is the code:

In the form, using the form helper:
<?php echo  $form->checkbox('my_attribute','my_value1',false); ?>
<?php echo  $form->checkbox('my_attribute','my_value2',false); ?>
<?php echo  $form->checkbox('my_attribute','my_value3',false); ?>

In my controller, to pick up the post data:
$myAttrib = explode(",",$this->post('my_attribute'));

In my model, to apply the data to the (newly created) page:

What obvious thing am I missing here?

Thanks, all.

View Replies: View Best Answer
jordanlev replied on at Permalink Reply
Try putting brackets around the attribute name in the form, like this:

<?php echo $form->checkbox('my_attribute[]','my_value1',false); ?>

I don't think the controller code needs the bracket though, so your code for that stays the same.
brennaH replied on at Permalink Best Answer Reply
In the interest of trying to be as "c5 friendly" as possible, I did some deep diving into the various attribute objects and functions. I didn't have any luck using setAttribute($obj,$value) as I was uncertain what type the $value was supposed to be for a multiple select object. An array didn't seem to do it. I ended up finding the saveAttributeForm($obj) function which worked beautifully.

I thought I would post what I ended up with in case other are trying to do the same thing. Unfortunately, Jordan, I didn't have any luck with the above suggestion, though it would have been a much quicker solution if I had.

To print the multiple select checkboxes form out:
$ak = CollectionAttributeKey::getByHandle('my_attribute'); //get attribute key object
$c = Page::getCurrentPage(); //get current page
$values = $c->getAttributeValueObject($ak); //get values of attribute for this page
echo $ak->render('form', $values, true); // print out form

In the controller, to capture and post the values (skipping the model layer, for simplicity here):
$page = Page::getByID($this->post('cID');
$ak = CollectionAttributeKey::getByHandle('my_attribute');
brianvandelden replied on at Permalink Reply
Hi brennaH,

This solution does not print a multiselect i'm looking for.

echo $ak->render('form', $values, true); prints out a <select> for just 1 option. Do you know a solution to generate a multiselect?

joseajohnson replied on at Permalink Reply
brianvandelden -

You may want to double check your package controller and make sure the declaration for your attribute key sets 'akSelectAllowMultipleValues' to true.

Pages and Themes > Attributes > Edit > Mutiple Values