Skip to content

Commit

Permalink
Add clear option to Flash Message
Browse files Browse the repository at this point in the history
To give user the option to disable Stacking of messages and being consistent with 3.x
  • Loading branch information
xhs345 committed Oct 31, 2016
1 parent 924d382 commit e1c5ef9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/Cake/Controller/Component/FlashComponent.php
Expand Up @@ -38,6 +38,7 @@ class FlashComponent extends Component {
'key' => 'flash',
'element' => 'default',
'params' => array(),
'clear' => false
);

/**
Expand Down Expand Up @@ -82,7 +83,10 @@ public function set($message, $options = array()) {
}
$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(
'message' => $message,
Expand Down
14 changes: 14 additions & 0 deletions lib/Cake/Test/Case/Controller/Component/FlashComponentTest.php
Expand Up @@ -131,6 +131,20 @@ public function testSet() {
$result = CakeSession::read('Message.foobar');
$this->assertEquals($expected, $result);
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');
}

/**
Expand Down

0 comments on commit e1c5ef9

Please sign in to comment.