I have the following situation. Posts hasMany Comments
Comments belongTo Posts
In my /Views/Posts/view, I display the Post with its Comments. Also, with every Post, a Comment Form should be displayed. Therefore, I have to use an Element add_comment.ctp to the View (correct me please if I am wrong, but see this question here).
/Views/Posts/view.ctp:
// add comments
echo $this -> element('add_comment',array('post_id' => $entry['Post']['id']), array('cache' => array('config' => 'long')));
The element:
/**
* Element for adding comments
*
*/
echo $this -> Form -> create('Comment', array('url' => array(
'controller' => 'comments',
'action' => 'add',
$post_id
)));
?>
<fieldset>
<legend><?php echo 'Add Comment'; ?></legend>
<?php
echo $this -> Form -> input('author_name');
echo $this -> Form -> input('author_email', array('type' => 'email required'));
echo $this -> Form -> input('author_website');
//echo $this->Form->input('date_published');
echo $this -> Form -> input('text');
//echo $this->Form->input('is_archived');
?>
</fieldset>
<?php echo $this -> Form -> end(array('label' => 'Post!')); ?>
As you can see, the form is submitted to the add action of CommentsController. Now, the big question: How can the add action actually pass data such as Validation results back to the form? I mean, form data should also be persisted, so in case somebody entered invalid data, its not lost.
Normally, the add action would render /View/Comments/add, but I neither need this view nor have I even defined one.
So far I have used $this->redirect to go back to the /Views/Posts/view after the Comment was saved -- but redirecting just calls /Views/Posts/view without passing anything. So how can I make use of Elements in Combination with smooth and automagic form handling?