繁体   English   中英

Cakephp应用程序中缺少验证消息

[英]Missing validation message in cakephp application

我正在尝试为cakephp应用程序添加布局,但现在不再显示我的验证消息。 验证博客条目的评论时,不会显示假定位于顶部的验证消息。

如果更改布局意味着您错过添加

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

之前

其他可能的位置在电流控制器中。

搜索是否有类似以下代码:

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

第一个代码负责显示消息,而第二个代码负责设置消息。

但是代码肯定会提供更多帮助:)

这是我在comment_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']);

        }

这是我的文章和评论所在的entry.ctp:

<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>

这是我的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'));
    }
}

我知道这很旧,但必须确保您的应用程序/视图/布局/default.ctp中有以下代码可见(或此应用程序的布局是什么)

<?php echo $this->Session->flash(); ?>

如果没有要显示的消息,它将不回显任何内容,但是如果有消息,则将相应地输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM