CodeIgniter Forums

Full Version: flashdata return buggy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]jwindhorst[/eluser]
I've been using flashdata to send errors when I need to to redirect a user to a new location with errors on the page. I use
Code:
session->set_flashdata('errors', array('error message goes here.'))

On the page that the user is redirected to, specifically in the controller, I have this code:
Code:
$errors=$this->session->flashdata('errors');

if(is_array($errors))
  $data['errors']=$errors;

I had to test to see if the result from flashdata was indeed an array. Even when errors is not set, the call to flashdata will indeed return "something" even if it is 'NULL'. So using isset, or even count on the return value will give you unexpected results.

It would be really cool if calling flashdata on an item that was not set returned something more programatically intuitive; like false for example.

Anyone know if there is a good reason for it behaving this way?

Thanks.

El Forum

[eluser]thinkigniter[/eluser]
I would try this next

Code:
session->set_flashdata('errors', array('error message goes here.'));

Too

Code:
session->set_flashdata( array('errors'=>'error message goes here.'));

El Forum

[eluser]Michael Wales[/eluser]
I'm pretty sure flashdata does return FALSE when that value has not been assigned. Which would mean isset() would return TRUE, because it is set - set to FALSE.


Using explicit comparison operators (=== and !==) will end these frustrations.

El Forum

[eluser]thinkigniter[/eluser]
Yep!

Thats right

From session.php

Code:
function userdata($item)
    {
        return ( ! isset($this->userdata[$item])) ? FALSE : $this->userdata[$item];
    }

El Forum

[eluser]jwindhorst[/eluser]
Now I see it! I don't know why it wasn't more apparent before (perhaps because it's not talked about in the docs?) anyway, yes, I got it to work in a much cleaner/happier fashing using the following:

Code:
// make sure that we have our expected flash information from request/add
    if($this->session->flashdata('requests')!==false)
    {  
      $test=$this->session->flashdata('requests');
    }

Thanks for the replies.