Welcome Guest, Not a member yet? Register   Sign In
Problem populating form after validation
#1

[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;
          $results['userdata'] = $userrow;
          $this->load->view('header');
          $this->load->view('transactedit', $results);

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(
        set_value('firstname'),


etc then this:

Code:
$results['userdata'] = $usertemp;
      $this->load->view('header');
      $this->load->view('transactedit', $results);

but when transactedit is called it reports:

Code:
A PHP Error was encountered
Severity: Notice

Message: Undefined index: firstname

Filename: views/transactedit.php

Line Number: 11

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.
#2

[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.
#3

[eluser]Unknown[/eluser]
My personnal solution with a unique form :

Code:
echo form_label('Username', 'username');
echo form_input('username', set_value('username', (isset($profile->username)?$profile->username:'')));


Step 1 : populate form from db $profile is set
Step 2 : from validation $profile is not set
#4

[eluser]markbarratt[/eluser]
Thanks, konogan - just what i need and once you've written it, it seems so obvious.




Theme © iAndrew 2016 - Forum software by © MyBB