CakePHP: Check if SPECIFIC flash message is set

cakephp, cakephp-2.0, php, session

Solution

Figured it out! This works:

$this->Session->check('Message.location_key')

It returns true/false depending on whether there are any such flash messages set. `->read()` does the same thing, but returns the flash data if there is (any and crucially, it leaves the session var so it can still be echoed later).

Problem

I have a page with several sections with forms that are submitted from the same page. The forms collapse to save space, but I want to conditionally keep them open if there is an error on submission. In my controller, I set a specific "key" (see `location_key` below) for each form, which allows me to echo them in their respective locations: In controller: ``` $this->Session->setFlash('You missed something...', 'element_name', array('class'=>'error'), 'location_key'); ``` In view: ``` $this->Session->flash('location_key') ``` I'm trying to figure out how to check if `$this->Session->flash('location_key')` exists. If I do this it works but unsets the flash message: ``` if ( $this->Session->flash('location_key') ) // = TRUE //Do something $this->Session->flash('location_key') // = FALSE (because it just got called) ``` How can I test for the presence of this flash message without causing it to go away?

Original source