2010-08-22 103 views
5

我正在構建一個網站,需要在單個頁面中以不同數字顯示同一模型的多個表單。這些表單屬於具有ID的對象。目前,由於我無法弄清楚如何更改表單標識,所以我被一堆重複的標識卡住了。如何使CakePHP的表單助手「創建」操作使用自定義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> 

回答

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']))); 
+0

讓我來檢查一下你的代碼,然後,因爲這個工作對我蠻好: <?php echo($ form-> create(「Option」,array(「id」=>「Test」)));?> 請參閱:codeacula.com並查看源代碼。 – Codeacula 2010-08-23 01:38:31

+0

更新了它。這是爲我工作的形式。如果它不適合你,那是超出我所知的。 – Codeacula 2010-08-23 01:44:40

+0

啊對不起。它確實有效。我有一個愚蠢的視線時刻。我正在查看錶單中的一個輸入,而不是表單本身。哇,我覺得很傻。謝謝你的幫助。 – 2010-08-23 02:45:35

相關問題