Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp-create a multi select form field

I need to create a multi select form field using form helpers in cakephp.The values in the field will get populated from a table which had got a HABTM to the current model.

What is the best way to implement this?

like image 713
huzefam Avatar asked Nov 24 '12 04:11

huzefam


1 Answers

In your ctp file:

echo $this->Form->input('Category', array(
    'multiple' => 'multiple',
    'type' => 'select',
));

in your action:

$cats = $this->Category->find('all');
foreach ($cats as $category) {
    $categories[$category['Category']['id']] = $category['Category']['title'];
}
$this->set(compact('categories'));
like image 133
Arash Mousavi Avatar answered Oct 20 '22 07:10

Arash Mousavi