Forms 如何在CakePHP中在一个页面上创建多个表单?

Forms 如何在CakePHP中在一个页面上创建多个表单?,forms,cakephp,cakephp-1.3,Forms,Cakephp,Cakephp 1.3,我正试图想出一种标准方法,在索引页上为同一模型创建多个表单。这里有更多的细节 我有一个活页夹列表,每个活页夹都有一个备忘录。我希望备忘录字段可以从索引页编辑。显然,仅复制和粘贴“编辑备忘录”操作的视图是行不通的,如下所示: <?php echo $this->Form->create('Binder');?> <fieldset> <legend><?php __('Edit Memo'); ?></leg

我正试图想出一种标准方法,在索引页上为同一模型创建多个表单。这里有更多的细节

我有一个活页夹列表,每个活页夹都有一个备忘录。我希望备忘录字段可以从索引页编辑。显然,仅复制和粘贴“编辑备忘录”操作的视图是行不通的,如下所示:

<?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));?>
但是没有运气。备注字段仍然会获得常规id,因此我想我可能也需要更改它。当我提交表单时,它会执行操作,但不会保存。仅供参考,我已将id参数路由到路由中的操作


我确信必须有一种标准的方法来在索引循环中呈现多个表单。有什么想法吗?

我想知道这个链接是否有助于解决您的问题:


Essential它建议创建两个新的(空的)模型来扩展您的基本模型,您的表单可以独立调用它们。似乎这是一种相对轻松的方式,可以让表单彼此区别开来……

我想知道此链接是否有助于解决您的问题:


Essential它建议创建两个新的(空的)模型来扩展您的基本模型,您的表单可以独立调用它们。似乎这是一种相对轻松的方式,可以让表单彼此区别开来……

这并不太难,但你必须少依赖Cake的FormHelper魔法。以下作品(或至少曾多次为我工作):



我不能完全确定您的数据结构,因此需要对上面的内容进行一些调整,但您应该了解这一点。我不知道创建虚假模型等有什么用。

这并不太难,但你必须少依赖Cake的FormHelper魔法。以下作品(或至少曾多次为我工作):



我不能完全确定您的数据结构,因此需要对上面的内容进行一些调整,但您应该了解这一点。我不知道创建虚假模型等有什么用。

丹尼尔,你完全正确!很不错的。这就是我最终得到的代码。这很重要,有一个主要原因。HTML规范要求ID必须是唯一的。因此,我在表单中添加了一个lil SUMSHIN和字段以防止出现问题。仍然有效

<?php
$baseUrl = array('controller'=>'binders','action'=>'edit_memo');
$url = $baseUrl; 
$url['id'] = $binder['Binder']['id'];

echo $this->Form->create(null,array(
    'id'=>"BinderEditMemo-{$binder['Binder']['id']}",
    'url'=>$url
));
?>
<fieldset>
    <legend><?php __('Memo'); ?></legend>
    <?php
    echo $this->Form->input('Binder.id', array('id' => "BinderId-{$binder['Binder']['id']}", 'type'=>'hidden','value'=>$binder['Binder']['id']));
    echo $this->Form->input('Binder.memo', array('id' => "BinderMemo-{$binder['Binder']['id']}", 'value' => $binder['Binder']['memo'], 'label' => '', ));
    ?>
</fieldset>             
<?php echo $this->Form->end(__('Update Memo',true)); ?>

丹尼尔,你完全正确!很不错的。这就是我最终得到的代码。这很重要,有一个主要原因。HTML规范要求ID必须是唯一的。因此,我在表单中添加了一个lil SUMSHIN和字段以防止出现问题。仍然有效

<?php
$baseUrl = array('controller'=>'binders','action'=>'edit_memo');
$url = $baseUrl; 
$url['id'] = $binder['Binder']['id'];

echo $this->Form->create(null,array(
    'id'=>"BinderEditMemo-{$binder['Binder']['id']}",
    'url'=>$url
));
?>
<fieldset>
    <legend><?php __('Memo'); ?></legend>
    <?php
    echo $this->Form->input('Binder.id', array('id' => "BinderId-{$binder['Binder']['id']}", 'type'=>'hidden','value'=>$binder['Binder']['id']));
    echo $this->Form->input('Binder.memo', array('id' => "BinderMemo-{$binder['Binder']['id']}", 'value' => $binder['Binder']['memo'], 'label' => '', ));
    ?>
</fieldset>             
<?php echo $this->Form->end(__('Update Memo',true)); ?>


完美。作为答案发布的最终代码。不确定这是否是在这里做事的正确/最佳方式。非常感谢。没问题。你可以自由地接受和接受/无耻的;)完美的作为答案发布的最终代码。不确定这是否是在这里做事的正确/最佳方式。非常感谢。没问题。你可以自由地接受和接受/无耻的;)
<?php
$baseUrl = array('controller'=>'binders','action'=>'edit_memo');
$url = $baseUrl; 
$url['id'] = $binder['Binder']['id'];

echo $this->Form->create(null,array(
    'id'=>"BinderEditMemo-{$binder['Binder']['id']}",
    'url'=>$url
));
?>
<fieldset>
    <legend><?php __('Memo'); ?></legend>
    <?php
    echo $this->Form->input('Binder.id', array('id' => "BinderId-{$binder['Binder']['id']}", 'type'=>'hidden','value'=>$binder['Binder']['id']));
    echo $this->Form->input('Binder.memo', array('id' => "BinderMemo-{$binder['Binder']['id']}", 'value' => $binder['Binder']['memo'], 'label' => '', ));
    ?>
</fieldset>             
<?php echo $this->Form->end(__('Update Memo',true)); ?>