2010-07-22 59 views
0

我試圖爲cakephp應用程序添加布局,但現在我的驗證消息不再顯示。在驗證對博客條目的評論時,不會顯示這些假設位於頂部的驗證郵件。在cakephp應用程序中缺少驗證消息

+3

代碼請問?這樣,我們可能知道什麼是錯的 – 2010-07-22 14:30:32

+0

正如君說的,你應該發佈相關的代碼。特別是驗證數組(或設置錯誤消息的任何地方),以及模板(ctp)中應顯示啓動器消息的部分。 – webbiedave 2010-07-22 14:35:04

回答

0

如果你改變佈局意味着你錯過了之前的

其他可能的地方加入

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

是在電流控制器。

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

第一個代碼負責顯示信息,而第二個是負責設置消息:

,如果你有這樣的代碼搜索。

但肯定的代碼將幫助更多:)

0

這裏是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']); 

     } 

這是我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')); 
    } 
} 
0

我知道這是舊的,但UST確保你有下面的代碼在您的應用程序/視圖/佈局/ default.thtml中顯眼的地方(或任何你爲這個應用程序佈局)

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

它如果沒有要顯示的消息,將不會回顯,但如果有消息,則會相應地輸出。