CakePHP 在 SELECT 输入中选择默认值
使用 CakePHP:
Using 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 框始终默认为 Tree #1,但我希望它默认为要添加到的 Tree:
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
的下拉框将默认为Tree 5",而不是当前默认为Tree 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()
, or text()
, or 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;
相关文章