Welcome Guest, Not a member yet? Register   Sign In
Simple form_validation question (CI 1.7 SVN)
#1

[eluser]Huan[/eluser]
I'm sorry if this has been asked elsewhere. I'm using CI 1.7's new form_validation library and I want to edit some data using an edit form. It has two fields "title" and "content" which I want to pre-load so that every time I open the edit form (first load) the fields are populated with data pulled from my database (using "SELECT title, content FROM tbl_name LIMIT 1"). My problem is I can't get it to display the data at first load. (After post back and $this->form_validation->run() has been called the data is displayed normally however).

Can somebody help? Thank you.
#2

[eluser]Pascal Kriete[/eluser]
Assuming you're following the userguide you probably have something like this:
Code:
<input type="text" name="title" value="<?php echo set_value('title'); ?>" size="50" />

The set_value() function takes two parameters. 1) The field name, and 2) a default. So all you need to do is pass your database results to your view and plop them into the second parameter.
Code:
<input type="text" name="title" value="<?php echo set_value('title', $title_from_db); ?>" size="50" />
#3

[eluser]Colin Williams[/eluser]
Quote:which I want to pre-load so that every time I open the edit form (first load) the fields are populated with data pulled from my database

This can basically be done with ANY version of CodeIgniter. You can pass data to your View file with the second parameter of the $this->load->view() function. So, why not just load the db values and pass em on to the View that contains your form? Is that too radical?
#4

[eluser]Sumon[/eluser]
[quote author="Huan" date="1221283839"]My problem is I can't get it to display the data at first load. (After post back and $this->form_validation->run() has been called the data is displayed normally however).[/quote]

This one might be a good controller skeleton for you
Code:
if($_POST)
{
  //assign validation stuff here.
  if($this->validation->run() == TRUE)
  {
     // success stuffs
     $data['title'] = "";
     $data['content'] = "";
  }
  else
  {
    $data=$_POST;
  }
}
else
{
//load default value from database
$some_info=$this->your_mode->get_information($some_param);
$data['title'] = $some_info['title'];
$data['content'] = $some_info['content'];
}
#5

[eluser]Huan[/eluser]
Thanks everybody. Actually I was using only one form for both creating and updating the data, so the preloaded data is different each time. With the old (current) validation class, I could use something like this:

<input type="text" name="title" value="<?=$this->validation->title?>" />

When I CREATE data, I don't need to set the value at first load, because $this->validation->title == blank
When I UPDATE data, I set the value at first load: $this->validation->title = $q->row()->title (pulled from database)
Data posted back in both cases returns the correct value. <?=$this->validation->title?> works well every time.

When I change to the new form_validation class, I can no longer do it. Setting $this->form_validation->title = $title results in an error. I found that I could use <?=set_value('title', $default_title)?> by assigning a value to $default_title at first load each time. But I was just wondering if this is the best way to do it.
#6

[eluser]Colin Williams[/eluser]
Again, I think the best way is to pass that values to the View that holds the form. Figure out what each value should be in the controller (either empty, a default, or loaded from a DB) and then pass that into the view. That way, all your view ever does is <?= $name ?> and doesn't have to fuss with the logic. I know they added the set_value() function, but I don't even see it as necessary (maybe if used in the controller, e.g. $data['name'] = set_value('name', $row->name)).
#7

[eluser]Huan[/eluser]
Colin: Yes it makes sense. I know I can always find something good when I ask my questions here. Thank you.
#8

[eluser]Pascal Kriete[/eluser]
[EDIT: should've refreshed]

I must be missing something. The validation library is pretty smart, all you need to do is work out the difference between 'create' and 'edit'.

Controller:
Code:
if ('create')
{
    $data = array('field' => '', 'field2' => '');
}
else
{
    $data = $model->get_current_values();
}

if ($this->validation->run() == FALSE)
{
    $this->load->view('myform', $data);
}
//...

View:
Code:
<input type="text" name="field" value="<?php echo set_value('field', $field); ?>" size="50" />
<input type="text" name="field2" value="<?php echo set_value('field2', $field2); ?>" size="50" />




Theme © iAndrew 2016 - Forum software by © MyBB