Saving A Multi-Select Field in a Custom Joomla 3.X Component

First we want to make sure the XML is set for your multi-select field. Here is an example of the field:

  label="Bands (multi-select)"
  <option value="nirvana">Nirvana</option>
  <option value="rhcp">Red Hot Chili Peppers</option>
  <option value="modest mouse">Modest Mouse</option>

Once this has been completed, we want to handle the data (array) being passed before saying. So let’s go to the views controller file!

If the postSaveHook() is not present, create it.

protected function postSaveHook(JModelLegacy &$model, $validData = array())

Inside this function we cant to take the multi-select value, which is an array and implode() it!

// band (multi-select) set format
  $data['bands'] = implode(',', $validData['bands']);

Make sure the function ends with $model->save($data);

After that, it should be saving properly in the database. Now to show it correctly whenever you edit the item again… We are going to head to the views model file.

Make sure the following is inside the protected function loadFormData()

protected function loadFormData(){
  // check the session for previously entered form data.
  $data = JFactory::getApplication()->getUserState('', array());
  if (empty($data)){
    $data = $this->getItem();

  // bands
    $data->bands = explode(',', $data->bands);

  return $data;

And just like that, we are done. That was easy right? Post in the comments below if you have any questions!