I created an array in the controller from a table :
$tasktemplateResults = $this->Tasktemplate->find('list');
And in the view I have a form helper that creates a drop down list:
echo $this->Form->input('Tasktemplate.id', array('options' => array($tasktemplateResults),'label' => 'Task'));
The html output is as follows:
<div class="input select">
<label for="TasktemplateId">Task</label>
<select name="data[Tasktemplate][id]" id="TasktemplateId">
<option value="1">Test task</option>
<option value="2">Second test task</option>
</select>
</div>
I would like to have this html output:
<div class="input select">
<label for="TasktemplateId">Task</label>
<select name="data[Tasktemplate][id]" id="TasktemplateId">
<option value="Test task">Test task</option>
<option value="Second test task">Second test task</option>
</select>
</div>
How do I get the name and value pairs the same as the array as shown above?
You can add parameters to the find 'list' method to specify which fields to use for the key and the value.
Example:
$usernameMap = $this->Article->User->
find('list', array('fields' => array('User.username', 'User.first_name')));
array in the controller
$tasktemplateResults = $this->Tasktemplate->find('list', array('fields' => array('Tasktemplate.name')));
and in view
echo $this->Form->select('task', $tasktemplateResults);
来源:https://stackoverflow.com/questions/6531112/how-do-you-create-a-select-with-both-value-and-the-label-the-same-from-an-array