我有一个包含多个部分的页面,其中包含从同一页面提交的表单。表单会折叠以节省空间,但如果提交时出现错误,我想有条件地让它们保持打开状态。
在我的 Controller 中,我为每个表单设置了一个特定的“键”(参见下面的 location_key
),这允许我在它们各自的位置回显它们:
在 Controller 中:
$this->Session->setFlash('You missed something...', 'element_name', array('class'=>'error'), 'location_key');
在 View 中:
$this->Session->flash('location_key')
我想知道如何检查 $this->Session->flash('location_key')
是否存在。如果我这样做,它会起作用,但会取消设置闪现消息:
if ( $this->Session->flash('location_key') ) // = TRUE
//Do something
$this->Session->flash('location_key') // = FALSE (because it just got called)
如何在不让它消失的情况下测试这条闪现消息是否存在?
最佳答案
想通了!这有效:
$this->Session->check('Message.location_key')
它根据是否设置了任何此类闪现消息返回真/假。 ->read()
做同样的事情,但返回闪存数据(如果有的话)(任何重要的是,它离开 session var,因此它仍然可以在以后回显)。
关于php - CakePHP:检查是否设置了 SPECIFIC flash 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20379785/