Elwhis
Elwhis

Reputation: 1261

cakephp select box with recursive 2

I have Tarifs, each tarif hasMany Price and Price also belongsTo UserGroup. So basically prices change when user's group is changed - doesn't matter that much.

The view looks like this

<?php echo $this->Form->create('Tarif');?>
    ...
        $i=0;
        foreach ($this->data['Price'] as $price) {

            echo "<tr><td>".$this->Form->input("Price.$i.price", array('label' => false))."</td>";
            echo "<td>".$this->Form->input("Price.$i.currency", array('label' => false))."</td>";
            echo "<td>".$this->Form->input("Price.$i.UserGroup.id", array('label' => false))."</td>";
    ...     

And I need the UserGroup.id input to display as a select, where each option displays group name and has its id as value. The user_group_id values are fine, but the are displayed in a text input. I've tried $this->Form->select and $this->Form->input(...,'type'=>'select') but both of them provided select boxes with no options. How do I set the input to do what I want? Thanks

Upvotes: 0

Views: 623

Answers (1)

Chuck Burgess
Chuck Burgess

Reputation: 11574

In your controller, you need to add:

$user_groups = $this->UserGroup->find('list');
$this->set(compact('user_groups');

Then in the view, you setup the drop down like:

<?php echo $this->Form->input('user_group', array('options' => $user_groups)); ?>

You can then add $user_groups as an option to any Form->input and it will become a dropdown when using:

array('options' => $user_groups)

Upvotes: 1

Related Questions