Reputation: 15269
Normally, the $this->Session->setFlash(__('My message.'));
Will output:
<div id="flashMessage" class="message">
My message.
</div>
How can I change that, so it'll output:
<p class="notification>
My message.
</p>
instead?
Upvotes: 6
Views: 22234
Reputation: 1
I found this workaround, using jquery.
I have this code in controller if($var1 == null) { $this->Flash->error(__('Error message')); }
In the /app/View/Layouts/default.ctp I put this code
$(document).ready(function() {
if($('#flashMessage').length) // Check if flashMessage exists
{
var message = $('#flashMessage').html(); // Copy text
// if I send html tags in the flash message,I use this code to print html
message = message.replace(/</g, '<');
message = message.replace(/>/g, '>');
if( $('#flashMessage').hasClass("error") ) // Check if it has the error class
{
// Create a bootstrap alert warning and set the message
$('#flashMessage').html("<div class='alert alert-warning' style='width:50%'>"+message+"</div>");
}
if( $('#flashMessage').hasClass("success") ) // Check if it has the sucess class
{
// Create a bootstrap alert sucess and set the message
$('#flashMessage').html("<div class='alert alert-success' style='width:50%'>"+message+"</div>");
}
}
});
Upvotes: 0
Reputation: 4330
According to cakephp flash message no need to handle id and class in your view. You can set id and class when you setFlash as easy way.
$this->Session->setFlash(__('My message.'), 'default', array('id' => 'flashMessage', 'class' => 'message'), 'message');
$this->Session->setFlash(__('My message.'), 'default', array('class' => 'notification'), 'notification');
In your view.
echo $this->Session->flash('message');
echo $this->Session->flash('notification');
Thats it.
Upvotes: 3
Reputation: 3468
I don't think the current answers are a good approach to flash messages in general.
The entire application should simply have $this->setFlash('my message');
and the parent view or layout should decide how to render it (even if it is by using an element).
The following code should be in the layout, perhaps in the <head>
section.
<?php
$flashMessage = $this->Session->flash();
if ( $flashMessage !== false) {
echo $this->element('my_custom_flash_element', array(
'message' => $flashMessage
));
}
?>
The layout captures it, and passes the flash message as a variable to the element 'my_custom_flash_element'.
Inside the element you can have all fancy rendering with css, and even cool stuff like toastr.js (which I personally love!)
Example: (my_custom_flash_element.ctp)
<script>
$(document).ready(function() {
<?php echo "toastr.warning('" . $message . "');"; ?>
});
</script>
Also check out this wonderful concept on transient flash messages by @dereuromark
Upvotes: 0
Reputation: 1061
in app/element create .ctp file (example : flash.ctp)
<div class="alert alert-<?= (isset($alert)? $alert : 'info' ) ?>">
<button type="button" class="close" data-dismiss="alert">×</button>
<center> <?= (isset($message)? $message : 'Something went wrong' ) ?></center>
</div>
and use
$this->Session->setFlash($message,'flash',array('alert'=>'info'));
define class,type in your css file to customize your Flash Message I hope this will help
Upvotes: 0
Reputation: 5001
If you look at the source code you will see that the second parameter of the SessionComponent method is the name of an element:
function setFlash($message, $element = 'default', $params = array(), $key = 'flash')
You can create a file in views/elements (or Views/Elements for Cake2) called for instance 'flash_notification.ctp'
with the following content:
<p class="notification">
<?php echo $message; ?>
</p>
and use
$this->Session->setFlash(__('My message.'), 'flash_notification');
Upvotes: 19