![]() |
Database populated form fields (e.g. edit mode) and form validation - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Database populated form fields (e.g. edit mode) and form validation (/showthread.php?tid=14063) |
Database populated form fields (e.g. edit mode) and form validation - El Forum - 12-16-2008 [eluser]onblur[/eluser] How are people managing the editing/updating of database records using CI form validation (v1.7) when validation fails? The scenario is: 1. retrieve record from database for editing (ie. editing form is pre-populated with data) 2. submit edited record 3. controller does form validation - one or more validation rules are not met 4. reload form and display **submitted** data, and display validation errors How are folks reloading the form with the submitted (edited) data? I can reload the form with the original unedited data, but I want to display the edited data so the user can correct mistakes rather than edit all over again. Database populated form fields (e.g. edit mode) and form validation - El Forum - 12-16-2008 [eluser]walrus_lt[/eluser] ... Database populated form fields (e.g. edit mode) and form validation - El Forum - 12-16-2008 [eluser]majidmx[/eluser] As far as I guess, you're replacing data to the default one which is loaded from database. if you use set_value method you can easily repopulate the data : controller : Code: function edit_news($news_id) and your view would look like this : Code: <input type="text" name="title" id="title" size="80" maxlength="255" value="<?PHP echo set_value('title' , isset($default['title'])?$default['title']:'' );?>" /> note that, you don't need to fetch the record from database again in update_news let me know if you can get it worked. Cheers, MajiD Fatemian Database populated form fields (e.g. edit mode) and form validation - El Forum - 12-17-2008 [eluser]onblur[/eluser] Thanks majidmx! Your solution is working for me. I had checked a couple of similar posts suggesting the ternary operator but I couldn't get it to work in my specific situation. You reply clarified a couple of things and now have it working well. Cheers! Database populated form fields (e.g. edit mode) and form validation - El Forum - 12-17-2008 [eluser]majidmx[/eluser] You're welcome. Glad to hear you have it working properly now. Take care, Database populated form fields (e.g. edit mode) and form validation - El Forum - 02-24-2009 [eluser]huston[/eluser] I am new to CodeIgniter and was frustrated by this same issue. I have been testing a different approach which seems to be working so far. First I extended the CI_Form_validation class to setup the _field_data when the request is not a POST. Then I added a method, set_default_value, to set the default values for the fields in _field_data. Code: class MY_Form_validation extends CI_Form_validation { Then I added a call to set_default_value to my controller to set the current value for a field. Code: $this->form_validation->set_default_value('email', '[email protected]'); Here is the example from the CI Form Validation documentation with this code added. form.php: Code: class Form extends Controller { And the code for the view remains unchanged from the CI documentation. myform.php: Code: <html> Database populated form fields (e.g. edit mode) and form validation - El Forum - 02-24-2009 [eluser]majidmx[/eluser] Hi Huston, Glad to hear from you, you'll love CI. That's exactly the way I used to do for the CI versions before 1.7 In 1.7 the validation class has changed a lot and I really prefer to use it in a way that I mentioned above. I mean : Quote:controller Quote:view Database populated form fields (e.g. edit mode) and form validation - El Forum - 02-24-2009 [eluser]huston[/eluser] majidmx, Thanks for your reply. I saw that technique but I am pretty lazy, I am planning on extending some of the form helpers so I don't need to use set_value at all from my view. The code is so repetitive that it seems like it could be easily managed by the framework. |