Welcome Guest, Not a member yet? Register   Sign In
set_value and populating a form with values from the database
#1

[eluser]everdaniel[/eluser]
Hi everyone!

I've been working on this issue for a few days and I have no clue what I might have doing wrong.

I need to re-populate a form with values from the database, so what I'm doing in my controller is:

Code:
$this->form_validation->set_rules('spo_address1', lang('sponsor_form_address1'), 'trim|required|xss_clean');
$this->form_validation->set_rules('spo_address2', lang('sponsor_form_address2'), 'trim|xss_clean');

if ($this->form_validation->run() == FALSE) {
  // User Clicked on Change Data button
  if ($this->input->post('btn_change')) {
    $id_sponsor = $this->input->post('id_sponsor');
    // Get returns an array ($resultset->row_array())
    $sponsor_data = $this->sponsor_model->get(array('id_sponsor' => $id_sponsor));
    foreach($sponsor_data as $k => $v) $template[$k] = $v;
  }
  $this->load->view('sponsor/apply_step1', $template);
}
else {
  // Post Sponsor Info
}

That code, is for my controller, and in my view file I have:

Code:
<?= form_input('spo_address1', set_value('spo_address1', isset($spo_address1) ? $spo_address1 : '')); ?>
<?= form_input('spo_address2', set_value('spo_address2', isset($spo_address2) ? $spo_address2 : '')); ?>

Result: both input boxes are empty when I load the page (the variables are set and they both have values in it).

I checked the set_value function in the Form_validation library, and I saw that the default value is returned only if the field name is not set in the _field_data array (which is built, if I'm correct, when the rules are set), and since I did set them, then the postdata is returned (which I don't have any since I'm loading them from DB).

Changing the line 732 in Form_validation.php file from:
Code:
if ( ! isset($this->_field_data[$field]))
to
Code:
if ( ! isset($this->_field_data[$field]['postdata']))
does the trick, but I don't know if this is correct.

Can someone here point me out in the right direction?

Thanks everybody!

(Using the latest version of CI from SVN, Revision 1596)


Messages In This Thread
set_value and populating a form with values from the database - by El Forum - 01-15-2009, 07:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB