Two Alert questions

I need some clarifications Alert related.

For example in the controller:


Yii::$app->session->setFlash('alert', 'My Alert');

In the view:


	<?= Alert::widget([

		'options' => [

			'class' => 'alert-warning',

			'closeButton' => 'false',

		],

			'body' => (Yii::$app->session->getFlash('alert')),

	]); ?>

Maybe the closebutton setting is wrong? The close buttun still remain.

Second question: is there a way to show alert widget only when a message is present?

EDIT: To show alert widget only when an alert message is present I don’t find any other way than this:

(in view)


<script type='text/javascript'>

	document.addEventListener('DOMContentLoaded', function() {

		Val = document.getElementById('w0').innerHTML.indexOf("Your alert message");

		if( Val != -1 ) {

			document.getElementById('w0').style.display = 'block';

		} else {

			document.getElementById('w0').style.display = 'none';

		}

	}, false);

</script>

"w0" is, in my case, the id of alert widget in dom:


<div id="w0" class="alert-warning alert fade in" style="display: block;">

Hi,

There is a solution for you:

  • ‘closeButton’ => null,

  • Show alert:

<?= Alert::widget([‘id’=>‘yourId’,…,‘style’=>‘display:none’,…

<script type=‘text/javascript’>…

click… $(’#yourId’).show();

Good luck.