Teej
Teej

Reputation: 12873

Confused with Populating Select field

I currently have this:

Hotels Controller

class HotelsController extends AppController {

    var $name = 'Hotels';

    function admin_add() {
        $this->set('hotel_categories', $this->Hotel->HotelCategory->find('list'));

        if ( ! empty($this->data)) {

            $this->data['Page']['title'] = $this->data['Hotel']['title'];
            $this->data['Page']['layout'] = 'index';

            if ($this->Hotel->saveAll($this->data)) {
                $this->Session->setFlash('Your hotel has been saved', 'flash_good');
                $this->redirect(array('action' => 'admin_add'));
            }
        }
    }

HotelCategory Model

class HotelCategory extends AppModel {
    var $name = 'HotelCategory';

    var $hasAndBelongsToMany = array(
        'Hotel' => array(
            'className' => 'Hotel'
        )
    );

Hotel Model

class Hotel extends AppModel {
    var $name = 'Hotel';
    var $hasAndBelongsToMany = array(
        'HotelCategory' => array(
            'className' => 'HotelCategory'
        )
    );

View

<div id="main">
        <h2>Add Hotel</h2>
        <?php echo $this->Session->flash();?>
        <div>
        <?php
        debug($hotel_categories);
        echo $this->Form->create('Hotel');
        echo $this->Form->input('Hotel.title');
        echo $this->Form->input('HotelCategory', array('options' => 'select', 'multiple' => 'checkbox'));
        echo $this->Form->input('Hotel.body', array('rows' => '3'));

        echo $this->Form->input('Page.meta_keywords');
        echo $this->Form->input('Page.meta_description');

        echo $this->Form->end('Save Hotel');
        ?>
        </div>
<!-- main ends -->
</div>

I can confirm that when I debug($hotel_categories); that there are values.

The problem I am having is that the $this->Form->input('HotelCategory', array('options' => 'select', 'multiple' => 'checkbox')) doesn't produce any options.

Upvotes: 0

Views: 273

Answers (2)

Leo
Leo

Reputation: 6571

That should be:

echo $this->Form->input('HotelCategory', array(
                                            'type' => 'select', 
                                            'multiple' => 'checkbox',
                                            'options'=>$hotel_categories));

Upvotes: 2

zeroSkillz
zeroSkillz

Reputation: 1468

try explicitly setting the options list in the view

<?php echo $this->Form->input('HotelCategory', array(
'type'=>'select', 
'options' => $hotel_categories, 
'multiple' => true)); ?>

Upvotes: 1

Related Questions