Flash data is session data that's only present until it has been retrieved. After retrieval, the session is destroyed. This can be very handy for display messages such as confirmation messages after form submits, or validation error messages. You can have as many flash entries as you like.
You can use the setFlashdata()
method to create flash data. This method belongs to the Webigniter class, so if you want to set some flash data in your custom form logic for example, you can use it like this.
public function contact_form(): bool
{
if($this->formData['name'] != "harry")
{
$this->webigniter->setFlashdata('error', 'You are not Harry!']);
return false;
}
return true;
}
This will add a flash entry with the name 'error' and value 'You are not Harry!' after the contact_form is submitted and the name is not Harry.
After you have set flash data, you need to be able to show it in your view files obviously.
Please remember that flash data will only be present until you retrieve it.
This is how you can get flash data in your views.
<?php
if($webigniter->getFlashData('error')):?>
<div class='error'>
<?=$webigniter->getFlashData('error');?>
</div>
<?php endif;?>
This piece of code first checks if there is an error, and if so, show it.