在下拉字段cakephp 3.0中选择特定值

时间:2016-07-06 11:57:26

标签: cakephp-3.0

在select下拉列表中,从下拉列表Cakephp3.0中的值中选择一个特定值。我使用下面的代码:

    $abc = 'india';
    echo $this->Form->input('location_id', 
['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false]);

国家名称列于下拉列表中,但我想选择特定值,该值设置为变量$ abc(即印度)。

3 个答案:

答案 0 :(得分:1)

试试这段代码:

使用

中的“默认”键
 $abc = array('1' => 'One','2'=> 'Two');
 echo  $this->Form->input('location_id',
                          'options' => $abc, 
                          default' =>$abc[1], 
                          ['empty' =>'Please select',
                          'required'=>'required', 
                          'class' => 'form-control',
                          'label'=>false]
                         ); 

其中$ abc [0]是您想要的项目的关键

喜欢这样:

$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('field', $options, array('default' => 'F'));

答案 1 :(得分:0)

使用form input helper选项。像这样:

$selected = 'india';
echo $this->Form->input('location_id', ['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false, 'value'=> $selected]);

建议 - 阅读文档一次,然后开始开发。你永远不会遇到这么简单的问题。每当你遇到任何问题时,请先参考文档。

答案 2 :(得分:0)

echo  $form->input('field_name', array(
            'type' => 'select',
    'options' => $arrayOfOptions, // typically set from $this->find('list') in controller 
    'label'=> 'Label name here',
    'value' => $arrProjectLeaderDetails['id'],  // specify default value 
    'escape' => false,  // prevent HTML being automatically escaped
    'error' => false,
    'class' => 'input_select_medium'
));