Hey everyone, I have a little problem - possibly something stupid but i
can't seem to fix it.
I have a blog and I'm displaying a comments form on it, when i fill the form
and press enter - it adds the data as expected. But if a field was empty and
isn't suppose to be empty, then it goes to the /comments/add page and
displays the errors.
So basically i want my form errors to be shown on the blog page, not in the
default /comments/add page.
I've tried to set a redirect to the page it came from but then is would just
show the setFlash error message from the comments_controller but not the
form field errors.
Anyways, this is my /views/posts/view.ctp:
<!-- Meta Keywords and description --><?php $this->layout = 'default_stripped';
?><?php echo $this->Html->meta('keywords',
$post['Post']['keywords'],array('inline' => false));
$this->Html->meta('description', $post['Post']['description'],
array('inline' => false));
$this->set('title_for_layout', $post['Post']['name']);?>
<!-- Blog post -->
<div class="blogpost">
<h1><?php echo $this->Html->link($post['Post']['name'],
array('controller' => 'post', 'action' => $post['Post']['url'])); ?></h1>
<div class="blogpost_info">
<div class="date">
<?php echo date( 'd.m.Y' ,
strtotime($post['Post']['created'])); ?>
</div>
<div class="comments">
<?php echo $post['Post']['comments']; ?> Comments
</div>
<div class="author">
by <?php echo $post['Post']['author']; ?>
</div>
<?php if($session->check('Auth.User.id')): ?>
<div class="author">
<?php echo $this->Html->link('Edit', array(
'controller' =>
'posts',
'action' =>
'edit',
'admin' =>
true, $post['Post']['id']));?> -
<?php echo $this->Html->link(__('Delete', true), array(
'action' =>
'delete',
'admin' =>
true, $post['Post']['id']),
null,
sprintf(__('Are you sure you want to delete %s?', true),
$post['Post']['name'])); ?>
</div>
<?php endif;?>
</div>
<div class="blog_post_content">
<?php echo $post['Post']['content']; ?>
</div>
</div>
<br />
<!-- Blog Comments -->
<div id="comments">
<?php foreach($post['Comments'] as $comment):?>
<div class="comment-item">
<span class="comment-name">
<?php echo $comment['name']?>
</span>
<span class="comment-date">
<?php echo $comment['created']?>
</span>
<div class="comment-message">
<?php echo nl2br($comment['message'], true)?>
</div>
</div>
<?php endforeach;?>
<!-- Blog Comment Form -->
<?php echo $this->Form->create('Comment', array('action' => 'add'));?>
<?php echo $this->Form->input('Comment.posts_id', array('type' => 'hidden',
'value' => $post['Post']['id']));?>
<?php echo $this->Form->input('Comment.name');?>
<?php echo $this->Form->input('Comment.email');?>
<?php echo $this->Form->input('Comment.message');?>
<?php echo $this->Form->end('Submit');?>
</div>
<div class="cleafix"></div>
And this is the /controllers/comments_controller.php 'add' method:
function add() {
if (!empty($this->data)) {
$this->Comment->create();
if ($this->Comment->save($this->data)) {
$this->Session->setFlash(__('The comment has been saved',
true));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('The comment could not be saved.
Please, try again.', true));
}
}
}
I'm posting the posts_id in the comments using the id from the post by
putting it in a hidden input. I was wondering if there is a safer way of
passing that value before saving the comment.
*Basically what it currently does is post the data to
example.com/comments/add, and when the data is valid (eg: not empty) it gets
saved and the user gets redirected to the page s/he was on. However if and
incorrect email or an empty input box causes the data not to validate, then
the user gets redirected to example.com/comments/add and the validation
errors show next to the input fields. Instead when data isn't valid, i want
it to display it on the blog view and not on example.com/comments/add*
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php