Reputation: 21
I want to give a Flash message a custom css class in Cakephp 3, there is my function in my (edit in this case) controller:
public function edit($id = null)
{
$this->viewBuilder()->layout('personalizado');
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success('Utilizador editado com sucesso.', 'default', ['class' => 'alert alert-success']);
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('Erro ao apagar utilizador, por favor tente de novo.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
I added the custom css claas in this line:
$this->Flash->success('Utilizador editado com sucesso.', 'default', ['class' => 'alert alert-success']);
but is not working, thanks in advance.
Upvotes: 1
Views: 5251
Reputation: 23
This one is works for me.
Go to src/Template/Element/
Create directory file called "Flash"
Put your success.ctp and error.ctp template in that directory file.
So you have 2 files called success.ctp and error.ctp in directory src/Template/Element/Flash/
put this script in your src/Template/Element/Flash/success.ctp
<div class="alert alert-success alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<?= strip_tags($message, '<br>') ?>
</div>
also, put this script in your src/Template/Element/Flash/error.ctp
<div class="alert alert-danger alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<?= strip_tags($message, '<br>') ?>
</div>
And next, add this flash script function in your Controller
$this->Flash->error(__("Your error message"));
return $this->redirect(['action' => 'index']);
for your error flash message or this bellow
$this->Flash->success(__("Your successed message"));
return $this->redirect(['action' => 'index']);
for your successed flash message.
Then, in your view.ctp add this script bellow
<?= $this->Flash->render() ?>
Dont forget to load FlashComponent in your AppController.php first.
More detail setting is in this page. Check it out.
Upvotes: 0
Reputation: 1413
I´m suppose you want load alert with boostrap class
If customizate success flash message you should go to src/Template/Flash/success.ctp
and add your classes
<div class="alert alert-success" onclick="this.classList.add('hidden')">
<strong><?= __('Success') ?>!</strong> <?= h($message) ?>
</div>
Upvotes: 0
Reputation: 5767
// In your Controller
$this->Flash->success('The user has been saved', [
'params' => [
'class' => 'alert alert-success'
]
]);
// In your View
<?= $this->Flash->render() ?>
<!-- In src/Template/Element/Flash/success.ctp -->
<div class="<?= h($params['class']) ?>">
<?= h($message) ?>
</div>
Upvotes: 4
Reputation: 60463
It doesn't work because that's not how it is supposed to work. Don't just throw in stuff and hope for the best, read the documentation first to figure out how things are ment to be done!
There simply is no class
option.
Cookbook > Controllers > Components > Flash > Setting Flash Messages
If you want to set a specific class for the success flash message, then you have to modify the flash message element that lives in src/Template/Element/Flash/success.ctp
, similar to as shown in the linked docs.
You can use the params
option to pass custom options and use them in your elements if you want to define things on controller level.
Upvotes: 3