Welcome Guest, Not a member yet? Register   Sign In
Validation errors when using redirect instead of loading a view...
#1

[eluser]jay2003[/eluser]
Hi,

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

Code:
$this->session->set_flashdata('errors', validation_errors());
redirect($redirect_to);

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.

Thanks

Jason
#2

[eluser]CroNiX[/eluser]
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.
#3

[eluser]InsiteFX[/eluser]
You could also try:
Code:
$this->session->keep_flashdata('item');

Which will keep it for another one!
#4

[eluser]Aken[/eluser]
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.
#5

[eluser]skunkbad[/eluser]
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.
#6

[eluser]jay2003[/eluser]
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.

Cheers

Jason




Theme © iAndrew 2016 - Forum software by © MyBB