Forms (show and process) on the same controller or on different ones? |
The question is - what is the best approach
to show froms (if post/get is empty) and process data(if post/get contains data) in one controller or to make different controllers(or in this particular case methods), and if data don't pass the validation redirect back to previous one? Which approach is better? If you do all in one controller(method) it become a mess, with lots of IF statements and curly bracers scattered all over the place☻
I keep everything in one controller and have a separate method for create, read, update and delete.
If the validation fail they should get to the same function/method for displaying error messages. If it works you can then redirect them. As this example, I redirect them to a specific update function that let's the user set more information about that group. Controller: PHP Code: <?php PHP Code: <?php
One Controller - Different Methods. Name the methods really clearly so you understand what is going on. Like showNewRegisterForm() shows a form that has not been filled in yet. validateSubmittedRegisterForm() - validates the form. If it fails validation then don't bury a bunch of IF ELSE code - send it to a new method called something like showValidationFailedForRegisterForm()
If it passes validation - again don't bury the next step in the same method - make a new method for whatever the next step is. This keeps your methods more compact and much easier to maintain. Also the initial url for the form call has to be public - but all your other methods in the controller can be private which helps security. One pattern is to have the form address be the default index method of the controller. In that index method do some basic checks to confirm your form has been submitted etc - and then route to the appropriate private method. You can also add a hidden field and value to your form - and check for it first. If that value is not there, then its probably some kind of bot so there is no need to validate the form at all.
Both the previous answers are contain good ideas IMHO.
I had been doing it all in one method, before the if/else fiasco finally wound me up enough that I thought it would just be easier to have two separate views and methods for update or create. (Although I would definitely keep it all in one controller). The additional repeated code is not too substantial. A pain though when the form keeps changing, having to update two views and two methods. The back button on the browser causes lots of work/problems as well, still have not found a reliable and cross-browser way to prevent the 'do you want to resend form data' browser message etc. Paul.
I haven't done so myself; But you can add some logic to a add_edit view so that it post to /save/id-here-if-available and save() then looks for an id-here-if-available. If it exists (and in database), call update() else create() and take the logic from there. So that you get rid of the big IF/ELSE.
As you will probably use set_value() you will need to make sure that you create empty sets in the create() function for the second parameter. Regarding 'do you want to resend form data' you will need to save all post data in $_SESSION and redirect the user. And use that $_SESSION data after the redirection have finished. https://en.wikipedia.org/wiki/Post/Redirect/Get
You can always extend default controller with new functionality. Codeigniter's documentation is the good place to start.
Here is a CRUD Controller Template for what they are talking about above.
PHP Code: <?php There's a good start. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |