Saving custom checkbox attribute with multiple values
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:
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?
<?php echo $form->checkbox('my_attribute','my_value1',false); ?> //etc...
I don't think the controller code needs the bracket though, so your code for that stays the same.
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:
Loader::model("attribute/categories/collection"); $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):
Loader::model("attribute/categories/collection"); $page = Page::getByID($this->post('cID'); $ak = CollectionAttributeKey::getByHandle('my_attribute'); $ak->saveAttributeForm($page);
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?
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