have you looked into flash data
they can be used for this purpose to show messages like success
, errors
and warnings
which are or are not specific to your model attributes. It's up to you where you want o use them.
I mostly insert data or save models within transaction
block and there when I am saving multiple models I use try catch block to get the errors for any models that occur and use session flash along with ArrayHelper
to add errors in the flash message.
For your purpose you can use it the following way.
Set the flash message with
Yii::$app->session->setFlash('error','you are not allowed to perform this message');
and you can get it using the following inside your view
if(Yii::$app->session->hasFlash('error')){
echo Yii::$app->session->gettFlash('error');
}
a more sophisticated approach towards this is kartikwidgetsAlertBlock
install it using composer
php composer.phar require kartik-v/yii2-widget-alert "*"
Then create a file called alerts.php
in layouts
folder with the following code
use kartikwidgetsAlertBlock;
AlertBlock::widget (
[
'useSessionFlash' => false ,
'type' => AlertBlock::TYPE_GROWL ,
'alertSettings' => [
'settings' => [
'type' => kartikwidgetsGrowl::TYPE_SUCCESS ,
'icon' => 'glyphicon glyphicon-ok-sign' ,
'title' => 'Note' ,
'showSeparator' => true ,
'body' => Yii::$app->session->getFlash ( 'success' )
] ,
]
] );
AlertBlock::widget (
[
'useSessionFlash' => false ,
'type' => AlertBlock::TYPE_GROWL ,
'alertSettings' => [
'settings' => [
'type' => kartikwidgetsGrowl::TYPE_INFO ,
'icon' => 'glyphicon glyphicon-ok-sign' ,
'title' => 'Note' ,
'showSeparator' => true ,
'body' => Yii::$app->session->getFlash ( 'info' )
] ,
]
] );
AlertBlock::widget (
[
'useSessionFlash' => false ,
'type' => AlertBlock::TYPE_GROWL ,
'alertSettings' => [
'settings' => [
'type' => kartikwidgetsGrowl::TYPE_DANGER ,
'icon' => 'glyphicon glyphicon-ok-sign' ,
'title' => 'Note' ,
'showSeparator' => true ,
'body' => Yii::$app->session->getFlash ( 'error' )
] ,
]
] );
AlertBlock::widget (
[
'useSessionFlash' => false ,
'type' => AlertBlock::TYPE_GROWL ,
'alertSettings' => [
'settings' => [
'type' => kartikwidgetsGrowl::TYPE_DANGER ,
'icon' => 'glyphicon glyphicon-ok-sign' ,
'title' => 'Note' ,
'showSeparator' => true ,
'body' => Yii::$app->session->getFlash ( 'danger' )
] ,
]
] );
AlertBlock::widget (
[
'useSessionFlash' => false ,
'type' => AlertBlock::TYPE_GROWL ,
'alertSettings' => [
'settings' => [
'type' => kartikwidgetsGrowl::TYPE_WARNING ,
'icon' => 'glyphicon glyphicon-ok-sign' ,
'title' => 'Note' ,
'showSeparator' => true ,
'body' => Yii::$app->session->getFlash ( 'warning' )
] ,
]
] );
and then include it in your layout file after $this->beginBody()
call like below
<?= Yii::$app->view->renderFile ( '@frontend/views/layouts/alerts.php' ); ?>
then you just have to set the flash message and that is it you won't even have to call getFlash()
, the extension will display it automatically you have the following variables available
- success
- info
- danger
- warning
- error
set using Yii::$app->session->setFlash('danger','You cannot do this');
EDIT:
Note: Whenever you are redirecting after setting the flash message remember to use return
along with redirect()
otherwise you may face the problem that the messages are not showing up.
return $this->redirect(['index']);
EDIT 2:
you are trying to add an error message for any particular model that is related to the model currently being saved and you want some soultion that would allow you to throw exceptions and show them in a nicely formatted error message, so the problem you are facing is setting the error message, if i would do it i would use the following approach. Lets say i have a actionTest()
like below which is saving the form on submission.
public function actionTest() {
$model = new Campaign();
if ($model->load(Yii::$app->request->post())) {
//start transaction block
$transaction = Yii::$app->db->beginTransaction();
try {
if (!$model->save()) {
//throw an exception if any errors
throw new Exception(implode("<br />",yiihelpersArrayHelper::getColumn($model->errors, 0,false)));
}
//commit the transaction if there arent any erorrs to save the record
$transaction->commit();
} catch (Exception $ex) {
//roll back the transaction if any exception
$transaction->rollBack();
//catch the error and display with session flash
Yii::$app->session->setFlash('danger',$ex->getMessage());
}
}
$this->render('test');
}