Commit cca34951 by Alexander Makarov

Fixes #3979: Alert widget in advanced app is now able to display multiple flash…

Fixes #3979: Alert widget in advanced app is now able to display multiple flash messages of the same type
parent b12f4dd7
...@@ -11,9 +11,17 @@ namespace frontend\widgets; ...@@ -11,9 +11,17 @@ namespace frontend\widgets;
* Alert widget renders a message from session flash. All flash messages are displayed * Alert widget renders a message from session flash. All flash messages are displayed
* in the sequence they were assigned using setFlash. You can set message as following: * in the sequence they were assigned using setFlash. You can set message as following:
* *
* - \Yii::$app->getSession()->setFlash('error', 'This is the message'); * ```php
* - \Yii::$app->getSession()->setFlash('success', 'This is the message'); * \Yii::$app->getSession()->setFlash('error', 'This is the message');
* - \Yii::$app->getSession()->setFlash('info', 'This is the message'); * \Yii::$app->getSession()->setFlash('success', 'This is the message');
* \Yii::$app->getSession()->setFlash('info', 'This is the message');
* ```
*
* Multiple messages could be set as follows:
*
* ```php
* \Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']);
* ```
* *
* @author Kartik Visweswaran <kartikv2@gmail.com> * @author Kartik Visweswaran <kartikv2@gmail.com>
* @author Alexander Makarov <sam@rmcreative.ru> * @author Alexander Makarov <sam@rmcreative.ru>
...@@ -47,19 +55,22 @@ class Alert extends \yii\bootstrap\Widget ...@@ -47,19 +55,22 @@ class Alert extends \yii\bootstrap\Widget
$flashes = $session->getAllFlashes(); $flashes = $session->getAllFlashes();
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
foreach ($flashes as $type => $message) { foreach ($flashes as $type => $data) {
if (isset($this->alertTypes[$type])) { if (isset($this->alertTypes[$type])) {
/* initialize css class for each alert box */ $data = (array) $data;
$this->options['class'] = $this->alertTypes[$type] . $appendCss; foreach ($data as $message) {
/* initialize css class for each alert box */
$this->options['class'] = $this->alertTypes[$type] . $appendCss;
/* assign unique id to each alert box */ /* assign unique id to each alert box */
$this->options['id'] = $this->getId() . '-' . $type; $this->options['id'] = $this->getId() . '-' . $type;
echo \yii\bootstrap\Alert::widget([ echo \yii\bootstrap\Alert::widget([
'body' => $message, 'body' => $message,
'closeButton' => $this->closeButton, 'closeButton' => $this->closeButton,
'options' => $this->options, 'options' => $this->options,
]); ]);
}
$session->removeFlash($type); $session->removeFlash($type);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment