dividedbyzero
dividedbyzero

Reputation: 181

Clearing Flash Message in CakePHP 3.1.1

I'm trying to clear a Flash Message in CakePHP 3.1.1

I have a function when a user logs in, if his customer data is not complete, he is redirected to a form to complete it. That looks like this:

public function index()
{   
    //Some code to check whether the customers profile is complete

    //If it's not complete, then redirect to the "complete" action with a flash message
    if($completion == 0){
       $this->Flash->success(__('Please complete your customer profile data.'));
        $this->setAction('complete', $custid);

    //Otherwise go to their view 
    } elseif ($completion == 1){
        $this->setAction('view', $custid);
    } 
}

This works fine, and user is redirected to the Complete action/Form with the Flash Message.

Then the Complete action looks like this:

public function complete($id = null)
{  
    //Get all the customer data input

    if ($this->request->is(['patch', 'post', 'put'])) {
        $customer = $this->Customers->patchEntity($customer, $this->request->data);
        //Set the completion status to complete (1)
        $customer->completion_status = 1;
        if ($this->Customers->save($customer)) {
            $completion = 1;
            $this->set('completion', $completion);

        //After the Save, redirect to the View action with a new Flash Message
            $this->Flash->set(__('Your customer information is complete and has been saved!',['clear'=>'true']));
            return $this->redirect(['action' => 'view',$custid]);
        } else {
            $this->Flash->error(__('The customer could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('customer'));
    $this->set('_serialize', ['customer']);
}

It work fine BUT: When the user is redirected to the View action with the Success Flash after saving their data, the Flash from the Index (telling them 'Please complete your customer profile data.') still shows up again.

If the user refreshes on the view, then both Flash messages go away as they should.

How can I clear that initial Flash message when redirecting? I've tried using the clear key but it seems to not be working.

Any advice is greatly appreciated! Thanks, DBZ

Upvotes: 1

Views: 4708

Answers (5)

Tobias Gaertner
Tobias Gaertner

Reputation: 1184

You can add this in the beginning of your action

$this->request->session()->delete('Flash');

to delete more specific you can do e.g. to delete only the messages from AuthComponent

$this->request->session()->delete('Flash.auth');

One more thing: you can handle which flash-messages are displayed in the view like:

this->Flash->render('auth');

or

this->Flash->render('error');

If you don't display a flash-message its kept in the session until you display it somewhere or remove it from session.

Upvotes: 2

Pradeep Kumar
Pradeep Kumar

Reputation: 1

New in version 3.1: Flash messages now stack. Successive calls to set() or __call() with the same key will append the messages in the $_SESSION. If you want to keep the old behavior (one message even after consecutive calls), set the clear parameter to true when configuring the Component.

Use like this:

$this->loadComponent( 'Flash', ['clear' => true] );

Upvotes: 0

Douglas Santos
Douglas Santos

Reputation: 96

Apparently you are passing a string to clear instead of a boolean:

New in version 3.1: A new key clear was added. This key expects a bool and allows you to delete all messages in the current stack and start a new one.

Try setting true without the quotation marks:

$this->Flash->set(__('Your customer information is complete and has been saved!'),[
    'clear'=> true
]);

Upvotes: 0

jb1
jb1

Reputation: 31

Check to make sure that you aren't always getting $completion == 0 (which could match FALSE as well).

I suspect this is why your flash message is always showing.

Cake will automatically delete a flash message after it displays it.

Upvotes: 0

ADmad
ADmad

Reputation: 8100

Flash message are stored in session, so just clear the relevant session key: $this->Session->delete('Flash.flash') or $this->Session->delete('Flash')

Upvotes: 1

Related Questions