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.
我有多对一的关系,让我们假装它是很多叶子到树。当然,我烘焙了一个表单来向树中添加Leaf,您可以使用表单助手创建的下拉框(标记)指定它是哪个树。
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,但我希望它默认为它被添加到的树:
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.
例如,调用example.com/leaf/add/5将调出接口以向Tree#5添加新的Leaf。 Leaf.tree_id的下拉框默认为“Tree 5”,而不是它当前默认的“Tree 1”。
What do I need to put in my Leaf controller and Leaf view/add.ctp
to do this?
我需要在Leaf控制器和Leaf视图/ add.ctp中放置什么来执行此操作?
9 个解决方案
#1
19
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;
#2
49
In CakePHP 1.3, use 'default'=>value
to select the default value in a select input:
在CakePHP 1.3中,使用'default'=> value选择select输入中的默认值:
$this->Form->input('Leaf.id', array('type'=>'select', 'label'=>'Leaf', 'options'=>$leafs, 'default'=>'3'));
#3
8
the third parameter should be like array('selected' =>value)
第三个参数应该是数组('selected'=> value)
#4
8
$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中的选项列表中选择默认的第二个索引位置值。
#5
1
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.
设置第三个参数以设置所选选项。
#6
0
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 Female
in the list box by default.
上面的代码默认会在列表框中选择Female。
Keep baking...
#7
0
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)
); ?>
#8
0
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及更高版本,则可以使用空属性在select输入中添加默认值,如下例所示。
echo $this->Form->input('category_id', ['options'=>$categories,'empty'=>'Choose']);
#9
0
The best answer to this could be
对此的最佳答案可能是
Don't use selct for this job use input instead
请勿将selct用于此作业使用输入
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.
希望能帮助到你。
#1
19
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;
#2
49
In CakePHP 1.3, use 'default'=>value
to select the default value in a select input:
在CakePHP 1.3中,使用'default'=> value选择select输入中的默认值:
$this->Form->input('Leaf.id', array('type'=>'select', 'label'=>'Leaf', 'options'=>$leafs, 'default'=>'3'));
#3
8
the third parameter should be like array('selected' =>value)
第三个参数应该是数组('selected'=> value)
#4
8
$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中的选项列表中选择默认的第二个索引位置值。
#5
1
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.
设置第三个参数以设置所选选项。
#6
0
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 Female
in the list box by default.
上面的代码默认会在列表框中选择Female。
Keep baking...
#7
0
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)
); ?>
#8
0
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及更高版本,则可以使用空属性在select输入中添加默认值,如下例所示。
echo $this->Form->input('category_id', ['options'=>$categories,'empty'=>'Choose']);
#9
0
The best answer to this could be
对此的最佳答案可能是
Don't use selct for this job use input instead
请勿将selct用于此作业使用输入
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.
希望能帮助到你。