如何在CakePHP中的单个页面上创建多个表单? [英] How can I create multiple forms on a single page in CakePHP?
问题描述
我试图想出一个标准的方法来为索引页上的同一模型创建多个表单。这里有一些细节。
I'm trying to come up with a standard way to create multiple forms for the same model on an index page. Here's a little more detail.
我有一个活页夹列表,每个活页夹都显示一个备忘录。我想要的备注字段可以从索引页面编辑。显然,它不工作只是复制和粘贴的edit_memo动作的视图,如下:
I have a list of binders, each with a memo being displayed. I would like the memo fields to be editable from the index page. Obviously it doesn't work to just copy and paste the view for the edit_memo action, like so:
<?php echo $this->Form->create('Binder');?>
<fieldset>
<legend><?php __('Edit Memo'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('memo');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
但这本质上就是我想要做的。
But that's essentially what I'm looking to do. In context, I'm just looping through the binders in the index action and the memos are part of the model.
我尝试改变了。在这种情况下, $ this-> Form-> create('Binder')
:
$this->Form->create(null,array(
'id'=>"BinderEditMemo.$i",
'controller' => 'binders',
'action' => 'edit_memo',
'id' => $binder['Binder']['id']
));
但没有运气。备注字段仍然获得常规ID,因此我想我可能需要更改它。当我提交表单它执行操作,但它不保存。 FYI,我已经将id参数路由到我的路由中的操作。
But no luck. The memo field still gets the regular id, so I think I might need to change that as well. When I submit the form it does perform the action, however it does not save. FYI, I have routed the id parameter to the action in my routes.
我确定在索引循环中必须有一个标准的方式来呈现多个表单。任何想法?
I'm sure there has to be a standard way to render multiple forms in the index loop. Any thoughts?
推荐答案
这不是太难,但你不得不依赖Cake的FormHelper魔法。以下作品(或至少为我工作了很多次):
This isn't too difficult, but you have to rely somewhat less on Cake's FormHelper magic. The following works (or has worked for me on a number of occasions, at least):
<?php
$baseUrl = array('controller'=>'binders','action'=>'edit');
foreach ($binders as $_binder) {
$url = $baseUrl; $url['id'] = $_binder['id'];
echo $this->Form->create('Binder',array('url'=>$url));
echo $this->Form->input('Binder.id', array('type'=>'hidden','value'=>$_binder['id']));
echo $this->Form->input('Binder.memo', array('value'=>$_binder['memo']));
echo $this->Form->end(__('Submit',true));
}
?>
我不确定你的数据的结构,所以上面将需要一些调整,但你应该得到的想法。我不知道目的是通过创建假模型等服务。
I'm not exactly sure of the structure of your data, so it the above will require some tweaking, but you should get the idea. I have no idea purpose what is served by creating false models and etc.
这篇关于如何在CakePHP中的单个页面上创建多个表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!