CodeIgniter Forums
Controller flow when processing forms - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Controller flow when processing forms (/showthread.php?tid=12402)



Controller flow when processing forms - El Forum - 10-17-2008

[eluser]vendiddy[/eluser]
Hi all! Is there a standard way that form processing is implemented in CodeIgniter? Here are the high-level steps I have in mind:

1. Display empty form
2. Submit
A. If there are validation errors, redisplay the form with the errors.
B. If there are no validation errors, display a success page.

How would I set up my controller methods? Would I have one method to display the view and another method to process the view? (Or would both go in the same method?)

Btw, I already know the basics of CodeIgniter (including the validation library).

Thanks!


Controller flow when processing forms - El Forum - 10-20-2008

[eluser]Aken[/eluser]
To put it simply without code:

1. Set validation rules and field names.
2. Check to see if the form validates.
3. If false (also the default), display the form w/ any errors.
4. If true, process the form then display a message, redirect the page, or whatever you want.


Controller flow when processing forms - El Forum - 10-20-2008

[eluser]Mirage[/eluser]
Here's default skeleton for a controller function that processes a form. I tend to post and get to the same action:

Code:
function contactus() {

    if($_SERVER['REQUEST_METHOD']=='POST'){
        $this->load->library('Validation');
        $this->load->config('cfg_forms');

        $this->validation->set_fields($this->config->item('form_contactus_fields'));
        $this->validation->set_rules($this->config->item('form_contactus_rules'));
        $this->validation->set_error_delimiters('<div>','</div>');

        if ($this->validation->run()) {
                // Do stuff with the form data

                // Choose to redirect here or show the same form again
        }

    }

    $this->load->view('contactus');

}

HTH
-m


Controller flow when processing forms - El Forum - 10-20-2008

[eluser]vendiddy[/eluser]
Thanks Aken & Mirage! That really cleared things up.