Edit vs update |
[eluser]JonoB[/eluser]
Hi all, I am (almost) brand new to MVC frameworks, and I'm just trying to get my head around the best way to handle edits and updates. I've set up a simple test to CRUD a database. The one part that I am not 100% sure is how I have handled the edit and update logic. I am using www.phpactiverecord.org as my ORM, as I am familiar with it from another project. I may switch over to CI ActiveRecord lib in due course. I kinda cobbled this code from the help docs - its part of the products controller. Basically, the edit function and view lets me look at a single product. Clicking submit calls the update function, which runs through the validation rules. If validation passes, then we update the database and redirect to the product listing. If validation fails, then we redirect back to the edit view so that the user can try again. Code: function edit($id = '') So, this all seems to work fine, but I have no idea if this follows 'good' practise or the conventions. Thanks for any advice that you can give.
[eluser]SitesByJoe[/eluser]
You're right. That works. Things you can do to clean up a little more: 1. Make and use the form_validation config file (config/form_validation.php) and use it like described in the user guide. 2. You can just have one function do both operations (edit and update) using this structure: Code: function edit()
[eluser]InsiteFX[/eluser]
I would add the second parameter to tell it to XSS clean the input data. Code: $this->input->post('id', TRUE); InsiteFX
[eluser]JonoB[/eluser]
Thank you for the replies. That (mostly) makes sense. The part that I dont get it how I combine the edit and update into one controller. At the moment, the controller index shows a table of all products, clicking on one of those invokes the edit function, with the ID of the product being passed in as a parameter. Does your suggestion mean that I should post back to the same form, validate the input, and then either reshow the same form (validation failed), or go the product/index (validation passed)? Code: function edit($id = '') |
Welcome Guest, Not a member yet? Register Sign In |