2012-11-28 74 views
0

我知道這個問題已被問了很多次,但沒有答案正在解決我的問題。首先,讓我顯示我在哪裏setFlash和echo $ this-> Session-> flash()。Flash訊息不顯示cakephp

這是我的附加功能:

if(!empty($this->request->data)) 
    { 
     $friend_id = $this->request->data['User']['id']; 
     $user_id = $this->Auth->User('id'); 
     $already_friends = $this->Group->findByIdAndFriend($user_id, $friend_id); 
     if($already_friends) 
     { 
      $this->Session->setFlash(__('You are already friends', true)); 
     } 
     else 
     { 
      $this->Group->create(); 
      $data = array($user_id, $friend_id); 
      if($this->Group->save($data)) 
      { 
       $this->Session->setFlash(__('You are now friends', true)); 
      } 
      else { 
       $this->Session->setFlash(__('Failed to add friend. Please try again', true)); 
      }    
     } 
     $this->redirect(array('action' => 'index')); 

這是我的佈局,它的必要組成部分:

<div id="header"> 
      <?php echo $this->Html->image('connect.jpg', array('alt' => 'My Image', 'id' => 'headerImage')) ?> 
      <div id="headerNavMenu"> 
       <div name="navTab" class="menuTab"> 
        <?php echo $this->Html->link('Me', array('controller' => 'users', 'action' => 'view', CakeSession::read("Auth.User.id")), array('class' => 'tabName')) ?>       
       </div> 
       <div name="navTab" class="menuTab"> 
        <?php echo $this->Html->link('My Schedule', array('controller' => 'newSchedules', 'action' => 'index'), array('class' => 'tabName')) ?> 
       </div> 
       <div name="navTab" class="menuTab"> 
        <?php echo $this->Html->link('My Group', array('controller' => 'groups', 'action' => 'index'), array('class' => 'tabName')) ?> 
       </div> 
       <div name="navTab" class="menuTab"> 
        <a href="#" class="tabName">Settings</a> 
       </div> 
      </div> 
      <div id="login"> 
       <?php echo CakeSession::read("Auth.User.firstName").' '.CakeSession::read("Auth.User.lastName").' ' ?> 
       <?php echo $this->Html->link('logout', array('controller' => 'users', 'action' => 'logout')) ?> 
      </div> 
     </div> 
     <div id="mainContent"> 
      <?php echo $this->Session->flash(); ?> 
      <?php echo $this->Session->flash('auth'); ?> 
      <?php echo $this->fetch('content') ?> 
     </div> 
    </div> 

除了這個事實,沒有閃光燈的消息顯示,這也似乎是驗證標準未被檢查,因爲特定的消息沒有出現。

我是cakePHP的新手,但我一直在關注博客教程以完成大部分工作。閃光燈消息用於工作,但現在甚至不適用於過去的模型。

這裏是我的驗證:

public $validate = array(
    'first_name' => array(
     'required' => array(
      'rule' => array('notempty'), 
      'message' => 'First Name cannot be blank', 
      'allowEmpty' => false, 
      'required' => true 
     ) 
    ), 
    'last_name' => array(
     'required' => array(
      'rule' => array('notempty'), 
      'message' => 'Last Name cannot be blank', 
      'allowEmpty' => false, 
      'required' => true 
     ) 
    ), 
    'email' => array(
     'required' => array(
      'rule' => array('notEmpty'), 
      'message' => 'Email cannot be blank', 
      'allowEmpty' => false, 
      'required' => true 
     ), 
     'valid' => array(
      'rule' => array('email', true),   
      'message' => 'This is not a valid email', 
      'required' => true 
     ), 
     'unique' => array(
      'rule' => array('isUnique'), 
      'message' => 'This email already exists', 
      'required' => true 
     ) 
    ), 
    'password' => array(
     'required' => array(
      'rule' => array('notempty'), 
      'message' => 'Password cannot be blank', 
      'allowEmpty' => false, 
      'required' => true 
     ), 
     'valid' => array(
      'rule' => array('minlength', 6), 
      'message' => 'Password has to be at least 6 characters', 
      'required' => true 
     ), 
     'password_has_to_be_alphanumeric' => array(
      'rule' => 'alphanumeric', 
      'message' => 'Password has to be alphanumeric', 
      'required' => true 
     ) 
    ) 
); 
+0

我傾向於同意simofox,並確保你包括會話組件,但是,如果你的調試設置爲1或2,那麼你應該收到一個非常有用的錯誤信息。類似於,調用非對象上的成員函數setFlash()。如果你沒有收到這條消息,那麼appController中的某個東西可能會覆蓋flash消息。需要嘗試的是在上面的控制器函數的頂部添加一條flash消息(如果(空),然後在消息類型pr($ this-> Session)下),並至少在數組中搜索目標消息以縮小的問題。 –

+0

我的調試設置爲2.奇怪的是一些閃光信息顯示出來。 appController確實有會話組件。不過,我的驗證標準仍然不起作用。 – eytanfb

+0

嘗試手動檢查驗證...你會得到什麼? $ this-> Group-> set($ this-> request-> data); pr($ this-> Group-> validates()) –

回答

0

我可以不加註釋,所以我來回答,但在你的add函數的4行你應該用小寫 $this->Auth->user('id');寫用戶。 主題檢查已添加會話組件到你的$組件數組在AppController

+0

$ this-> Auth-> User('id')似乎適用於我的其餘邏輯 – eytanfb