Reputation:
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.
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.
I've tried changing $this->Form->create('Binder')
to the following:
$this->Form->create(null,array(
'id'=>"BinderEditMemo.$i",
'controller' => 'binders',
'action' => 'edit_memo',
'id' => $binder['Binder']['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?
Upvotes: 1
Views: 7654
Reputation:
Daniel, you were exactly right! Very nice. This is the code I ended up with. It's important for one main reason. The HTML spec says that ID's need to be unique. So I added a lil sumthin to the form, and fields to prevent problems. Still works.
<?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)); ?>
Upvotes: 0
Reputation: 4604
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.
Upvotes: 4
Reputation: 2580
I wonder if this link would help solve your problem: http://bakery.cakephp.org/articles/RabidFire/2010/06/26/multiple-forms-per-page-for-the-same-model
Essential it suggests creating two new (empty) models that extend your base model, which your forms can call on independently. Seems like it could be a relatively hassle-free way of keeping your forms distinct from one another...
Upvotes: 1