php CakePHP 在 SELECT 输入中选择默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1545764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:02:16  来源:igfitidea点击:

CakePHP select default value in SELECT input

phpcakephprelationshipshelperscakephp-2.x

提问by erjiang

Using CakePHP:

使用 CakePHP:

I have a many-to-one relationship, let's pretend it's many Leafs to Trees. Of course, I baked a form to add a Leaf to a Tree, and you can specify which Tree it is with a drop-down box ( tag) created by the form helper.

我有一个多对一的关系,让我们假设它是许多叶子到树。当然,我烘焙了一个表单,将叶子添加到树中,您可以通过表单助手创建的下拉框(标签)指定它是哪个树。

The only thing is, the SELECT box always defaults to Tree #1, but I would like it to default to the Tree it's being added to:

唯一的问题是,SELECT 框始终默认为 Tree #1,但我希望它默认为要添加到的 Tree:

For example, calling example.com/leaf/add/5would bring up the interface to add a new Leaf to Tree #5. The dropdown box for Leaf.tree_idwould default to "Tree 5", instead of "Tree 1" that it currently defaults to.

例如,调用example.com/leaf/add/5将调出接口以将新叶子添加到树 #5。的下拉框Leaf.tree_id将默认为“树 5”,而不是当前默认为“树 1”。

What do I need to put in my Leaf controller and Leaf view/add.ctpto do this?

我需要在我的 Leaf 控制器和 Leaf 中放入什么view/add.ctp才能做到这一点?

回答by Ryan

In CakePHP 1.3, use 'default'=>valueto select the default value in a select input:

在 CakePHP 1.3 中,用于'default'=>value在选择输入中选择默认值:

$this->Form->input('Leaf.id', array('type'=>'select', 'label'=>'Leaf', 'options'=>$leafs, 'default'=>'3'));

回答by Miles Johnson

You should never use select(), or text(), or radio()etc.; it's terrible practice. You should use input():

您永远不应该使用select(), 或text(), 或radio()等;这是可怕的做法。你应该使用input()

$form->input('tree_id', array('options' => $trees));

Then in the controller:

然后在控制器中:

$this->data['Leaf']['tree_id'] = $id;

回答by Sadikhasan

 $this->Form->input('Leaf.id', array(
'type'=>'select',
'label'=>'Leaf',
'options'=>$leafs,
'value'=>2
));

This will select default second index position value from list of option in $leafs.

这将从 $leafs 的选项列表中选择默认的第二个索引位置值。

回答by Renjith Chacko

the third parameter should be like array('selected' =>value)

第三个参数应该像 array('selected' =>value)

回答by Funky Dude

Assuming you are using form helper to generate the form:

假设您正在使用表单助手来生成表单:

select(string $fieldName, array $options, mixed $selected, array $attributes, boolean $showEmpty)

Set the third parameter to set the selected option.

设置第三个参数以设置所选选项。

回答by Yasin

cakephp version >= 3.6

cakephp 版本 >= 3.6

echo $this->Form->control('field_name', ['type' => 'select', 'options' => $departments, 'default' => 'your value']);

回答by rakeysharyal

If you are using cakephp version 3.0 and above, then you can add default value in select input using empty attribute as given in below example.

如果您使用的是 cakephp 3.0 及更高版本,那么您可以使用空属性在选择输入中添加默认值,如下例所示。

echo $this->Form->input('category_id', ['options'=>$categories,'empty'=>'Choose']);

回答by M.suleman Khan

The best answer to this could be

最好的答案可能是

Don't use selct for this job use input instead

不要对这个工作使用选择,而是使用输入

like this

像这样

echo  $this->Form->input('field_name', array(
          'type' => 'select',
            'options' => $options_arr, 
            'label' => 'label here',
            'value' => $id,  // default value
            'escape' => false,  // prevent HTML being automatically escaped
            'error' => false,
            'class' => 'form-control' // custom class you want to enter
        ));

Hope it helps.

希望能帮助到你。

回答by Haroon

To make a text default in a select box use the $form->select()method. Here is how you do it.

要在选择框中设置文本默认值,请使用该$form->select()方法。这是你如何做到的。

$options = array('m'=>'Male','f'=>'Female','n'=>'neutral');

$form->select('Model.name',$options,'f');

The above code will select Femalein the list box by default.

上面的代码Female默认会在列表框中选择。

Keep baking...

继续烤...

回答by Zimmo

FormHelper::select(string $fieldName, array $options, 
array $attributes)

$attributes['value']to set which value should be selected default

$attributes['value']设置应该选择哪个值默认

<?php echo $this->Form->select('status', $list, array(
    'empty' => false, 
    'value' => 1)
); ?>