Add clear option to Flash Message

To give user the option to disable Stacking of messages and being consistent with 3.x
This commit is contained in:
xhs345 2016-10-30 20:39:00 -07:00
parent 924d382bb3
commit e1c5ef9e7a
2 changed files with 19 additions and 1 deletions

View file

@ -38,6 +38,7 @@ class FlashComponent extends Component {
'key' => 'flash', 'key' => 'flash',
'element' => 'default', 'element' => 'default',
'params' => array(), 'params' => array(),
'clear' => false
); );
/** /**
@ -82,7 +83,10 @@ class FlashComponent extends Component {
} }
$options['element'] = $plugin . 'Flash/' . $element; $options['element'] = $plugin . 'Flash/' . $element;
$messages = CakeSession::read('Message.' . $options['key']); $messages = array();
if ($options['clear'] === false) {
$messages = (array)CakeSession::read('Message.' . $options['key']);
}
$newMessage = array( $newMessage = array(
'message' => $message, 'message' => $message,

View file

@ -131,6 +131,20 @@ class FlashComponentTest extends CakeTestCase {
$result = CakeSession::read('Message.foobar'); $result = CakeSession::read('Message.foobar');
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
CakeSession::delete('Message.foobar'); CakeSession::delete('Message.foobar');
$this->Flash->set('This is the first message');
$this->Flash->set('This is the second message', array('clear' => true));
$expected = array(
array(
'message' => 'This is the second message',
'key' => 'flash',
'element' => 'Flash/default',
'params' => array()
)
);
$result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result);
CakeSession::delete('Message.flash');
} }
/** /**