Welcome Guest, Not a member yet? Register   Sign In
Form Validation > Redirect instead of loading a view?
#1

[eluser]jpschroeder[/eluser]
Hello,

I've decided to start using CI's built in form validation rather than my own flavor, but I have a couple concerns about how these are handled, primarily in the area of error handling. The userguide presents this code as an example of how a form should be validated:

Code:
if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }

My issue with this code is that all my forms submit to a custom form controller /form/__tablename__

So for example, I could have the following:

example.com/edit/_sometable_/1

and it submits to:

example.com/form/_sometable_

Obviously it would be bad practice to load the form view from this controller since its not even in the same class as the original, causing the urls to "seem" confused (the form wold appear on form/_sometable_ when it was originally loaded on edit/_sometable_/1). What I want to do is redirect back to the original form page if the validation fails and present the errors to the user there. Of course, the validation_errors() will no longer be accessible after a redirect. I understand the structure of this form submission may seem strange, but it makes my code much much more dynamic to do it this way.

This is somewhat confusing, so if needed just ask and I'll explain further. Thanks in advance for any help.
#2

[eluser]David Johansson[/eluser]
Read about the function redirect in the URL helper:
http://ellislab.com/codeigniter/user-gui...elper.html
it should do just that.
#3

[eluser]jpschroeder[/eluser]
Thanks David,

I don't think I've quite communicated what the issue is. I am having no problem redirecting the page, that works just fine. The issue is I want the validation_error() values to be accessible on the page after the redirect. Since the values in form_validation live in the runtime of php, they are only accessible on that single execution of the script. In oldschool php I would use the $_SESSION variable to carry the errors back to previous page.

Would the best method to carry errors from page to page be the use of the flashdata session variable?
#4

[eluser]David Johansson[/eluser]
[quote author="jpschroeder" date="1248734617"]Thanks David,

I don't think I've quite communicated what the issue is. I am having no problem redirecting the page, that works just fine. The issue is I want the validation_error() values to be accessible on the page after the redirect. Since the values in form_validation live in the runtime of php, they are only accessible on that single execution of the script. In oldschool php I would use the $_SESSION variable to carry the errors back to previous page.

Would the best method to carry errors from page to page be the use of the flashdata session variable?[/quote]

I'm sorry, I must not have read carefully...

I think the best way is, as you suggest, to use the flashdata.

Code:
$this->session->set_flashdata('validation_errors', validation_errors());
$this->session->set_flashdata('form_error_field1', form_error('field1'));
$this->session->set_flashdata('form_error_field2', form_error('field2'));
$this->session->set_flashdata('form_error_field3', form_error('field3'));
$this->session->set_flashdata('set_value_field1', set_value('field1'));
$this->session->set_flashdata('set_value_field2', set_value('field2'));
$this->session->set_flashdata('set_value_field3', set_value('field3'));

And so on...
But don't forget to load sessions in both classes Smile

Maybe otherwise you could serialize all of the post array and store it in the flash data and then load it back replacing the original post-array and do the form validation once more. but i don't know if that's really a good way.
#5

[eluser]jpschroeder[/eluser]
ok thanks. That is what I've ended up doing!
#6

[eluser]darkhouse[/eluser]
Just my 2 cents. I wrote a custom library to handle messages, such as errors, success, notices and warnings. The way I do things is like this:

Code:
//controller

function index(){
   $this->load->view('my_form');
}

function submit(){
   if($this->form_validation->run()){
      //process data
      //set success message in my messages library, which is set to flashdata
      redirect('some_page');
   }
   //set errors to my custom messages library
   $this->index();
}

As you can see, I only redirect if the form is successful, otherwise I call the original method that loads the form. Again, just my 2 cents.
#7

[eluser]garycocs[/eluser]
That set_flashdata is the business just fixed a problem I was having with multiple forms in a large page, I didn't want to repeat loading all my views plus the code involved in them, wanted the redirect option.


THANK YOU!!!!




Theme © iAndrew 2016 - Forum software by © MyBB