Search code examples
phpyii

How do we unset setFlash in Yii?


I am using the following method and I am wondering if it's possible to remove the flash message.

Yii::app()->user->setFlash($key, $message);

I discovered that a certain message shows up even if it shouldn't, so I am wondering if there's a way to remove the message manually in Yii. I don't want to delete all the message, but only the message with the key 'error'.

I took a look at the official website, but there's no mention of how to remove a particular flash message, nor on how to remove all of them.


Solution

  • To remove a flash with key 'error', reset corresponding flash to 'null':

    Yii::app()->user->setFlash('error', null);
    

    As you can see in the source:

    web/auth/CWebUser.php#L682

    If the value provided equals the defaultValue (null) it will unset the flash message.