• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation errors when using redirect instead of loading a view...


I am creating a small CMS and am giving the user the ability to add items such as contact form and login form etc to any page which means that for the validation errors I have to use a redirect instead of loading a view (and hence losing the validation errors and form contents errors during the redirect).

I have googled and it looks like i have to use flashdata but am unable to get it to work.

Does anyone have an example of this?

I have tried

$this->session->set_flashdata('errors', validation_errors());

Where $redirect_to is the page the form was submitted from.

It is redirecting but losing the values and not showing an error.

I am pretty new to php and CI so any help would be much appreciated.



Flashdata lasts for exactly 1 request then it disappears (by design). Make sure the request isn't encountering any 404's or other redirects or that will count as the request and dump the flashdata.

You could also try:

Which will keep it for another one!

What you do is regardless of where the form is, the action is sent to its own page, such as mysite.com/login or mysite.com/contact

Then, if the form doesn't process properly, it loads that page with that specific form, along with any appropriate errors and/or info.

Then, you can save their originating page if you'd like to redirect back upon successful submission.

Have you guys noticed the amount of people that ask questions about setting a cookie immediately before a redirect? Is this really normal? I never do this, and it seems like the wrong thing to do. Nevertheless, I always direct people to the PHP docs, which state that the redirect needs to be before the cookie being set in the code. Don't know if this is the way it works in CI, but for OP it's worth a shot. CI is PHP after all.

Got it sorted thanks.

I was expecting it to somehow magically load the errors but when i took some time away from the computer realised I had to edit my form code to show the errors from the session and also in my controller to load the values to session and retrieve if the session is set and populate the form.



Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.