![]() |
Validate an edit form - 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: Validate an edit form (/showthread.php?tid=9371) Pages:
1
2
|
Validate an edit form - El Forum - 06-23-2008 [eluser]EEssam[/eluser] Hi, I need to validate an edit form, the logic as follow: 1. Load the data into text boxes from the database. 2. If the user submit the form with an empty field(s), the form show its details exactly as it was submitted. I was able to use the Validation class for adding records, but the thing is how can I populate the form before the it is submitted from the database and then populate it using the validation class after first submission? Please help. Validate an edit form - El Forum - 06-23-2008 [eluser]EEssam[/eluser] Is that not feasible? >:-( Validate an edit form - El Forum - 06-23-2008 [eluser]bavander[/eluser] Hi, I populate the validation values from the database if the form is initially loaded to edit (e.g. nothing was posted) so they show up in the inputs. Then when the form is posted, the values come from the validator, so any changes the user made are reflected. Not sure if this is a recommended approach, but has worked for me. Cheers, Bruce Validate an edit form - El Forum - 06-23-2008 [eluser]EEssam[/eluser] Thanks Bruce but do you have two views to handle this? If not, how do your edit view looks? Validate an edit form - El Forum - 06-23-2008 [eluser]nanda[/eluser] Here's the solution I use for my edit forms, it seems to work pretty good so far and does what I think you are asking for (without using 2 views). Code: <?php This checks if the validation variable is set first (if it is then the form must have been submitted, so this picks up changes), if it hasn't it takes whatever was stored in the database. I normally have the code all on one line, but I've broken it up below to be easier to read. I pass a $data array in the controller to the view, the mysql result for my event data is stored in $data['event'] so you get the $event object as referenced below. I'm probably going to put this in a plugin or helper for my own use as I like cleaner code in my views. Validate an edit form - El Forum - 06-23-2008 [eluser]ontguy[/eluser] Here is another approach: in your controller: Code: //if the form has not been submitted in your view: Code: <?=$this->validation->name ?> Validate an edit form - El Forum - 06-23-2008 [eluser]EEssam[/eluser] Thanks guys, I'll use ontguy solution. Validate an edit form - El Forum - 08-30-2008 [eluser]lostsock[/eluser] Update: Found the solution elsewhere, http://ellislab.com/forums/viewthread/69084/P15/#452197 Hey ontguy, I'm looking to use a solution like this in my app. Unfortunately the host I'm with at the moment only has PHP 4 and the solution you have offered doesn't seem to work. It seems to let me set $this->validation->name = "Anything" in the controller and I can even reference it later in the controller but when I echo $this->validation->name in the view that is loaded I get the following error: Code: A PHP Error was encountered Here is the code: controllers/test.php Code: function test() { Code: <?php Any thought would be very much appreciated. Validate an edit form - El Forum - 08-30-2008 [eluser]m4rw3r[/eluser] The new form_validation class (in SVN 1.7) has some neat functions for this. Validate an edit form - El Forum - 08-31-2008 [eluser]Phil_B[/eluser] I usually use this approach. Controller Code: function test($id=0) View Code: <form method="post" action="<?=$_SERVER['REQUEST_URI'];?>"> Works well. |