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

[eluser]Future Webs[/eluser]
http://ellislab.com/forums/viewthread/97495/

that post may give you a bit of help

Im still learning myself though too :-)
#3

[eluser]everdaniel[/eluser]
[quote author="w3bm" date="1232131856"]http://ellislab.com/forums/viewthread/97495/

that post may give you a bit of help

Im still learning myself though too :-)[/quote]

hey w3bm!

I found out what the problem is a few hours ago, it turns out that the rules are set if there are data in the $_POST var, if there is none which is the most common scenario (since most of the time you click a link to go to an edit page), the rules are not set and the set_value function works (since the fields are not set in the form_validation library).

Problem is, I'm not going to the edit page through a link, I'm using a submit button, means the $_POST var is not empty, rules are set, fields are set, set_value doesn't work because it tries to return the postdata value (which is empty).

I'm not sure if this is a bug or is an "error of logic" on my end... Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB