tags:

views:

31

answers:

2

I'm trying to add a layout for a cakephp application but now my validation message is no longer being displayed. When validating a comment on a blog entry, the validation message thats suppose to be at the top is not displayed.

A: 

If you changing the layout means that you missed to add

<?php
if ($this->Session->check('Message.flash')){
  echo $this->Session->flash();
}
?>

before the

Other possible place is in the current controller.

Search if you have code like:

$this->Session->setFlash('...');

The first code is responsible for displaying the message, while the second one is responsible for setting the message.

But the code definitely will help more :)

Nik
A: 

Here is my add function in comments_controller.php

function add(){
    debug($this->data);
    //if the user submitted a comment post
    if (!empty($this->data)){

        //display the 'add view'    
        $this->Comment->create();

        if ($this->MathCaptcha->validates($this->data['Comment']['security_code'])) {

                if ($this->Comment->save($this->data)){
                    $this->Session->setFlash(__('The Comment has been added.', true));
                    $this->redirect('/posts/index/'.$this->data['Comment']['post_id']);
                }
                //failed validation
                else{
                    debug($this->data);
                    $this->Session->setFlash(__('Comment could not be saved. Please try again.', true));

                }

        } 
        else {
            $this->Session->setFlash(__('Please enter the correct answer to the math question.', true));
            $this->redirect('/posts/index/'.$this->data['Comment']['post_id']);

        }

Here is my entry.ctp where my posts and comments reside:

<div id="article">


<h2><?php echo $entry[0]['Post']['title']; ?></h2>
<p class="date"><em>Modified:</em> <?php $date = new DateTime($entry[0]['Post']['modified']); 
    echo $date->format('Y-m-d');?></p>
<p class="date"><em>Author:</em> <?php echo $entry[0]['User']['username']; ?></p>


<p class="intro"><?php echo $entry[0]['Post']['content']; ?></p>

<h2>Comments:</h2>
<div id="comments_success"></div>

<!-- show the comment -->


<?php

        echo $form->create('Comment', array('action' => 'add'));

        echo $form->input('name', array('class' => 'validate[required] text-input'));
        echo $form->input('email', array('class' => 'validate[required,custom[email]] text-input'));

        echo $form->input('text', array('id' => 'commenttext', 'type' => 'textarea', 'label' => 'Comment:', 'rows' => '10', 'class' => 'validate[required] text-input'));

        //captcha
        echo $form->input('security_code', array('label' => 'Please Enter the Sum of ' . $mathCaptcha));
        echo $form->input( 'Comment.post_id', array( 'value' => $entry[0]['Post']['id'] , 'type' => 'hidden') ); 

        echo $form->end('Submit');

    ?>

<!-- comments -->

<ol>

<?php
    foreach ($entry[0]['Comment'] as $comment) :                
?>
    <li>
        <h3><?php echo $comment['name']; ?></h3>    
        <p class="date"><em>Date:</em> <?php echo $comment['created']; ?></p>
        <p class="text"> <?php echo $comment['text']; ?></p>    
    </li>       
<?php
    endforeach; 
?>              
</ol>
</div>

This is the index function in my posts_controller

function index($entry_id = null) {

    if (isset($entry_id)){
        $entry = $this->Post->findAllById($entry_id);

        $comments = $this->Post->Comment->getCommentsFromPostID($entry_id);

        $this->set('entry' , $entry);
        $this->set('mathCaptcha', $this->MathCaptcha->generateEquation());
        $this->render('entry');

    }
    else{

        $posts = $this->Post->find('all');  

        $this->set(compact('posts'));
    }
}
vanessa