How to implement select multiple listbox

Does activeListBox support implementation of a select MULTIPLE listbox?

Sure I can add an htmlOption for ‘multiple’ to go with activeListBox. This will give me a select multiple listbox in the form. However, the activeRecord class does not seem to support saving the array of selected values that returns with $_POST.

Maybe not suprising because it’s not clear how to store this array in MySQL (with a SET column maybe or as different rows in a HASMANY table)

Any hint?

Simple approach:

  1. Save selected values in a varchar column as comma separated string (‘15,23,45’)

  2. Add public virtual attribute to your AR (to be used as attribute for your dropdown):


 public $selection;



  1. afterFind(): explode your db column and put result into your virtual

public function afterFind() {

  $this->selection=explode(',',$this->dbcolumn);

}

  1. beforeSave(): implode your selection

public function beforeSave() {

  $this->dbcolumn=implode(',',$this->selection);

}

Oh, and don’t forget to make “selection” a safe attribute, while “dbcolumn” doesn’t have to be anymore.

Thanks Mike!

(although it took me some hours to find out that I had to add ‘return true’ to these functions to actually make it all work, but hey that’s how you learn stuff :) thanks again)

Heh, sorry, forgot that.

No problem at all. I’m glad you were at help.

Hello All,

I see that this post has not been visited in over a year. So, I am hoping to assist any future visitors that may come across this. Please view this URL:

LarryUllman.com

It is a fantastic guide for linking your model forms with related objects.

Nice post everyone. It seems like everyone wants to win in this post. Everyone trying to best from their sides.

thanks for this useful post!