【问题标题】:Flash messages doesn't show up cakephpFlash 消息不显示 cakephp
【发布时间】:2012-11-28 04:11:42
【问题描述】:

我知道这个问题已经被问过很多次,但没有一个答案能解决我的问题。首先让我展示一下我在哪里设置了 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 的新手,但我一直在按照博客教程完成大部分工作。以前的 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
        )
    )
);

【问题讨论】:

  • 我倾向于同意 simofox 并确保您包含会话组件,但是,如果您的调试设置为 1 或 2,那么您应该会收到一个非常有用的错误消息。类似于,调用非对象上的成员函数 setFlash()。如果您没有收到此消息,那么您的 appController 中的某些内容可能会覆盖 Flash 消息。可以尝试的方法是在 if(!empty) 上方的控制器函数顶部添加一条 Flash 消息,然后在消息类型 pr($this->Session) 下,至少在数组中搜索目标消息以缩小范围问题。
  • 我的调试设置为 2。奇怪的是,一些 Flash 消息出现了。 appController 确实有会话组件。但是我的验证标准仍然不起作用。
  • 尝试手动检查验证...你得到了什么? $this->Group->set($this->request->data); pr($this->Group->validates())
  • 我从验证中一无所获
  • 我们可以在模型中看到您的验证规则吗?

标签: cakephp flash-message


【解决方案1】:

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

【讨论】:

  • $this->Auth->User('id') 似乎适用于我的其余逻辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-29
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
相关资源
最近更新 更多