在cakephp下拉列表中添加默认选项“Please Select”

时间:2014-01-22 05:13:33

标签: cakephp selection default-value

我是cakephp的新手。我需要添加一个默认的

<option value="0">--Please Select--</option>

在我的以下选择字段中:

$attributes = array("empty"=>false,"Selected" => 'Select City',"id" => "location");
echo $form->select("gal_location_id", $gal_locations,null,$attributes); 

我尝试添加

$gal_locations[0] = "--Select City--";
$attributes = array("empty"=>false,"default" => 0,"id" => "location");

但选项位于列表底部。添加默认选项的正确方法是什么?

3 个答案:

答案 0 :(得分:14)

您正在寻找“空”属性:

$this->Form->input('gal_location_id', array(
    'type' => 'select',
    'options' => $gal_locations,
    'empty' => 'Select City', // <-- Shows as the first item and has no value
    'id' => 'location'
));

答案 1 :(得分:0)

this post

中查看CakePHP3中的等效内容

CakePHP3

使用以下选项,您不会收到选择下拉项目的通知。

因此,如果您只是寻找默认值并强制用户选择另一个,只需将字符串赋值为'empty'。

echo $this->Form->input('db_field', [
  'label' => 'My Drop Down',
  'empty' => [$default => $default], //Your default options
  'options' => $my_options //Your array /list'
]);

答案 2 :(得分:0)

echo $this->Form->input('country_id',[
    `enter code here`'options' =>$country,
    'label' => false,
    'class'=>'form-control select2',
    'empty'=> 'Select...',
    'value' => ''
]);