In my joomla custom component I used the SQL field type (item.xml):
<field name="colors" type="sql" query="SELECT id ,name FROM #__products_colors" multiple="multiple" key_field="id" value_field="name" class="inputbox"
label="colors" description="COM_PRODUCTS_FORM_DESC_ITEM_COLORS" />
In my view I call the field like this:
<?php echo $this->form->getInput('colors'); ?>
Which gives me a nice and smooth selectbox like this:
<select id="jform_colors" class="inputbox" multiple="multiple" name="jform[colors][]" aria-invalid="false">
<option value="1">blue</option>
<option value="2">yellow</option>
<option value="3">red</option>
<option value="4">green</option>
<option value="5">purple</option>
When I save this colors field, after selecting blue and red for example it gets saved as 1,3 in my database. Joomla does all the work for me... (THANKS Joomla)
Now maybe I am getting greedy, but somehow I expect Joomla to preselect these values for me when I edit an entry after saving. It does this with every other type of field, so why not here? Is there anything Im forgetting?
Thanks in adv!
edit: the bind function in the answer, I tweaked it a bit.
public function bind($array, $ignore = '') {
if (isset($array['params']) && is_array($array['params'])) {
$registry = new JRegistry();
$registry->loadArray($array['params']);
$array['params'] = (string) $registry;
}
//print_r($array);
if (key_exists('colors', $array) && is_array($array['colors'])) {
echo "pwn";
$array['colors'] = implode(',', $array['colors']);
}
if (isset($array['metadata']) && is_array($array['metadata'])) {
$registry = new JRegistry();
$registry->loadArray($array['metadata']);
$array['metadata'] = (string) $registry;
}
return parent::bind($array, $ignore);
}
And DONT use filter="safehtml" :)
Good luck all!