![]() |
Problem populating form after 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: Problem populating form after validation (/showthread.php?tid=22889) |
Problem populating form after validation - El Forum - 09-23-2009 [eluser]markbarratt[/eluser] Apologies for what is probably a beginner query: I'm pretty new to both PHP and CI. I'm having problems repopulating a form following validation failure. Here's what I am doing: (I need the user to confirm or amend details held in two db records in different tables). In controller A 1: retrieve existing records 2: turn them into row arrays 3: send the data to the form view like this: Code: $results['transaction'] = $transact; 4: the form view points to controller B In the form the data from controller A renders like this: Code: <input type="text" name="firstname" size="50" class="form" value="<?php echo set_value('firstname', $userdata['firstname']); ?>" /><?php echo form_error('firstname'); ?> etc. 5: On submission to controller B, I first test with validation rules: Code: $this->form_validation->set_rules('firstname', 'First name', 'trim|required'); then if the validation fails, 6: I try to put the form data back in the same data structure as it was first assembled in controller A so I can resend it to the form view. I do this: Code: $usertemp = array( etc then this: Code: $results['userdata'] = $usertemp; but when transactedit is called it reports: Code: A PHP Error was encountered where the offending line is Code: <input type="text" name="firstname" size="50" class="form" value="<?php echo set_value('firstname', $userdata['firstname']); ?>" /><?php echo form_error('firstname'); ?> (and so on for each field). What dumb thing am I doing? Thanks for any help. Problem populating form after validation - El Forum - 09-23-2009 [eluser]markbarratt[/eluser] sort-of solved: Populated the form from db retrieval as originally posted, then . in the processing controller, abandoned attempts to recreate array objects and . sent the validation-fail case to a new form view without without the 'default' object-reference in its set_value functions. So in the first form (populated from a db call) a typical input had Code: value="<?php echo set_value('shadr4', $transaction['shadr4']); ?>" In the second form (called by the validating controller it just returns the form values through the magic of CI and the equivalent code is Code: value="<?php echo set_value('shadr4'); ?>" Works fine, though seems inelegant. Problem populating form after validation - El Forum - 10-01-2009 [eluser]Unknown[/eluser] My personnal solution with a unique form : Code: echo form_label('Username', 'username'); Step 1 : populate form from db $profile is set Step 2 : from validation $profile is not set Problem populating form after validation - El Forum - 10-01-2009 [eluser]markbarratt[/eluser] Thanks, konogan - just what i need and once you've written it, it seems so obvious. |