我知道这个问题已被多次询问,但没有一个答案正在解决我的问题。首先让我展示我的setFlash和echo $ this-> Session-> flash()。
这是我的添加功能:
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的新手,但我一直在关注博客教程来完成我的大部分工作。 Flash消息曾经起作用,但现在它对以前的模型都不起作用。
以下是我的验证:
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 :(得分:0)
我无法添加评论,所以我必须回答,但在你的添加功能的第4行你应该用小写编写用户
$this->Auth->user('id');
。
主题检查将Session组件添加到AppController