Skip to content
Permalink
Browse files

Flash messages stack and in cases can result in duplicate messages wh…

…en a message is set in AppController::beforeFilter

then a controller receives a post and has to redirect. The message will be set twice.
If a duplicate => false is set, and the message is already in the array, it will not be added.
  • Loading branch information...
visualex committed Dec 6, 2016
1 parent f8ea642 commit 08e17edc91b2e04258495f7a474995c41bee9fb2
@@ -47,7 +47,8 @@ class FlashComponent extends Component
'key' => 'flash',
'element' => 'default',
'params' => [],
'clear' => false
'clear' => false,
'duplicate' => true
];
/**
@@ -111,6 +112,14 @@ public function set($message, array $options = [])
$messages = (array)$this->_session->read('Flash.' . $options['key']);
}
if ($options['duplicate'] === false) {
foreach ($messages as $existingMessage) {
if ($existingMessage['message'] == $message) {
return;
}
}
}
$messages[] = [
'message' => $message,
'key' => $options['key'],
@@ -108,6 +108,13 @@ public function testSet()
];
$result = $this->Session->read('Flash.foobar');
$this->assertEquals($expected, $result);
$this->Flash->config('duplicate', false);
$this->Flash->set('This test message should appear once only');
$this->Flash->set('This test message should appear once only');
$result = array_slice($this->Session->read('Flash.flash'), -2);
$this->assertNotEquals($result[0], $result[1]);
}
/**

0 comments on commit 08e17ed

Please sign in to comment.
You can’t perform that action at this time.