0

我知道这个问题已经被问过很多次了,但没有一个答案能解决我的问题。首先让我展示一下我在哪里设置了 Flash 并回显 $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 的新手,但我一直在关注博客教程来完成我的大部分工作。闪存消息过去可以工作,但现在它甚至不适用于以前的模型。

这是我的验证:

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
        )
    )
);
4

1 回答 1

0

我无法添加评论,所以我必须回答,但在你的 add 函数的第 4 行你应该用小写写 user $this->Auth->user('id');。主题检查是否已将 Session 组件添加到 AppController 中的 $components 数组

于 2012-11-28T07:57:57.133 回答