Best pratice form Form with validation errors - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: Best pratice form Form with validation errors (/showthread.php?tid=68460) |
Best pratice form Form with validation errors - michaelv - 07-13-2017 Hello first sorry for my english i'm upgrading a site from ci 2.1.6 to ci 3.1.5 the previous dev made 2 functions in a controller for form handeling 1 for loading the login view and 1 for the login process if a form_validation with errors occured we were redirected by redirect() from the login_process function to the first login_view in CI2 form validation errors where available after redirect, now IN CI3 it doesn't work anymore with this kind of setup was this a bad practice? how do we have to recode this stuff? only one controller as on the doc? i tried this kind of code: <?php class Form extends CI_Controller { public function index() { $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); if ($this->form_validation->run() == FALSE) { $this->load->view('myform'); } else { $this->load->view('formsuccess'); } } } my problem is that the first view (index in the above sample code) is after a post form, so when we are redirected to the the page we have a form validation error showwing how can i fix that? RE: Best pratice form Form with validation errors - skunkbad - 07-13-2017 Your guess is as good as any. What I mean is, 10 different people can tell you 10 different ways of doing this, so as a developer you should just come up with your own strategy. For me: 1) I post to the same method that generated the form/view. 2) Form validation and processing only occurs if CSRF token is good and request method is POST. 3) Errors, or a success message is sent to the view using $this->load->vars(). 4) It is not desirable to have all forms repopulate, and in that case simply kill the post vars. |