Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make cakePHP's form helper 'create' action use a custom id?

I'm building a site that requires multiple forms for the same model in varying numbers throughout a single page. These forms belong to an object with an id. Currently, since I can't figure out how to change the form ids, I'm stuck with a hole bunch of duplicate ids.

I'm looking for a way to append the object id to the form id so they're not invalid. I'm prefer to write my own javascript so I will not use the ajax helper.

<?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>
like image 981
Robert Hurst Avatar asked Oct 15 '22 01:10

Robert Hurst


1 Answers

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

That should do the trick, I believe.

EDIT:

After review, this is what I used to get what you were asking:

echo($form->create('Comment', array('action' => 'createComment', "id" => "form_".$object['Object']['id'])));
like image 73
Codeacula Avatar answered Oct 18 '22 11:10

Codeacula