Saving custom checkbox attribute with multiple values
Permalink
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:
In my controller, to pick up the post data:
In my model, to apply the data to the (newly created) page:
What obvious thing am I missing here?
Thanks, all.
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:
$page->setAttribute('my_attribute',$myAttrib);
What obvious thing am I missing here?
Thanks, all.
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:
In the controller, to capture and post the values (skipping the model layer, for simplicity here):
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);
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?
Thanks!
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?
Thanks!
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
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
I don't think the controller code needs the bracket though, so your code for that stays the same.