Welcome Guest, Not a member yet? Register   Sign In
Pre-populated form with validation class
#1

[eluser]Unknown[/eluser]
Hi,

I have an "edit" page for a form and am using the validation class, but can't figure out how to display the initial value retrieved from the database. If I have to assign the value of the validation to the input, what's a good way to pre-populate the field with the initial value?

view:
Code:
echo form_input('name', $this->validation->name);

controller:
Code:
public function edit() {

    $this->load->model('Company_model');
    $this->load->library('validation');

    $vrules['id'] = 'required';
    $vrules['name'] = 'trim|required';
    $this->validation->set_rules($vrules);

    $vfields['id'] = 'ID';
    $vfields['name'] = 'Name';
    $this->validation->set_fields($vfields);

    if($this->validation->run()) {

        // update db and do stuff
    
    } else {

        $id = $this->uri->segment(3);
        $data['query'] = $this->Company_model->get_single($id);

        // display form
        $this->load->view('header', $data);
        $this->load->view('company/edit');
        $this->load->view('footer');

    }
}
#2

[eluser]Unknown[/eluser]
Well, I got it to work using the following in my else code block:

Code:
$data['query'] = $this->Company_model->get_single($id);
$this->validation->id = $data['query']->id;
$this->validation->name = $data['query']->name;

Although, if there's a better way or if there might problems with this way, I'd love to hear.

Thanks!
#3

[eluser]Merolen[/eluser]
From another post I got this tips. I've set up each input field as an array

Code:
$inputField = array(
      'name'        => 'dateOfBirth',
      'maxlength'   => '4',
      'size'        => '6',
      'value'        => $this->validation->dateOfBirth ? $this->validation->dateOfBirth : $DBarray['dateOfBirth '],
        );

This goes for both input fields and select boxes. Checkboxes and radiobuttons I haven't quite figured out yet
#4

[eluser]Skuja[/eluser]
look in here
And comon man, try use the search feature of this forum. This problem has been discused lots of times.




Theme © iAndrew 2016 - Forum software by © MyBB