CakePHP在SELECT输入中选择默认值 [英] CakePHP select default value in SELECT input
问题描述
使用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.
唯一的事情是,SELECT框总是默认为树#1,但我想它默认为要添加到的树:
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:
,调用 example.com/leaf/add/5
会打开一个新的叶子到树#5的界面。 Leaf.tree_id
的下拉框将默认为树5,而不是其当前默认的树1。
For example, calling example.com/leaf/add/5
would bring up the interface to add a new Leaf to Tree #5. The dropdown box for Leaf.tree_id
would default to "Tree 5", instead of "Tree 1" that it currently defaults to.
我需要放在我的Leaf控制器和Leaf view / add.ctp
中做什么?
What do I need to put in my Leaf controller and Leaf view/add.ctp
to do this?
推荐答案
您不应该使用 select()
或 text c>或
radio()
等;这是可怕的做法。您应该使用 input()
:
You should never use select()
, or text()
, or radio()
etc.; it's terrible practice. You should use input()
:
$form->input('tree_id', array('options' => $trees));
然后在控制器中:
$this->data['Leaf']['tree_id'] = $id;
这篇关于CakePHP在SELECT输入中选择默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!