如何使cakePHP的表单帮助'创建'操作使用自定义ID?

时间:2010-08-22 23:49:28

标签: php forms cakephp-1.3 models

我正在构建一个网站,在整个单一页面中,不同数量的同一模型需要多个表单。这些表单属于具有id的对象。目前,由于我无法弄清楚如何更改表单ID,因此我遇到了一堆重复的ID。

我正在寻找一种方法将对象id附加到表单ID,这样它们就不会无效。我更喜欢编写自己的javascript,所以我不会使用ajax帮助器。

<?php

/**
 * This is a simplified example of what I am trying to do.
 */

?>

<div id="objects">
  <?php foreach($objects as $object): ?>
    <div class="object">
      <?php echo "this is object {$object['Object']['id']}"?>
      <?php
        //The following element would show a number of comments the object owns
        echo $this->element('object_comments_loop', array('comments' => $object['Object']['Comments']);
      ?>
      <div class="comment">
        <?php
          //each object has a form.
          //TODO: this is where the id issue comes into play.

          echo $form->create('Comment', array('url' => array('controller' => 'comments', 'action' => 'createComment'));
          echo $form->hidden('object_id', array('value' => $object['Object']['id']));
          echo $form->input('comment_body', array('label' => __('comment', true), 'type' => 'text'));
          echo $form->end(__('comment_submit', true));
        ?>
      </div>
    </div>
  <?php endforeach; ?>
</div>

1 个答案:

答案 0 :(得分:4)

echo $form->create('Comment', array('url' => array('controller' => 'comments', 'action' => 'createComment'), "id" => "form_".$object['Object']['id']));

我相信应该这样做。

编辑:

经过审核,这就是我过去常常得到的:

echo($form->create('Comment', array('action' => 'createComment', "id" => "form_".$object['Object']['id'])));