Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter's set_value() not re-populating

I've checked and re-checked my code, referencing the CI docs and other posts throughout the web, but I am unsuccessful at implementing the set_value() method for re-populating form fields after failed validation. Perhaps I am missing something very fundamental to the CI framework (I'm rather new to it), but your insight would be much appreciated.

I have the following in my controller method:

public function form_step2(){
    //Form Setup
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->library('form_validation');
    $data['title'] = $this->base_title;
    $data['base_url'] = base_url();

    //Validation Settings - must be set per step
    $this->form_validation->set_rules('request_type', 'Request Type', 'required');
    *...more of the same set_rules()*


    if ($this->form_validation->run() === FALSE) {
        ### Validation failed or New Form

        // Get form element data
        $data['request_types'] = $this->my_model->get_form_data('request_type');
        *...more of the same get_form_data() calls for loading form elements*

        //Generate Page from View Templates
        $this->load->view('templates/header', $data);
        $this->load->view('templates/form_step2', $data);
        $this->load->view('templates/footer');
    } else {
        ### Save to database
        $this->my_model->set_data($data);

        redirect('my_model/success','refresh');
    }
}

And in my view, a snippet of the code that is not re-populating:

<?php echo form_open('my_model/form_step2', array('class'=>'form-inline')); ?>

<label for="fname">First Name</label>
<input type="text" id="fname" name="fname" value="<?php echo set_value('fname'); ?>" />
<input type="submit" name="submit" value="Submit" />

I can't figure this one out, so thanks in advance for your help.

like image 867
Julian Soro Avatar asked Oct 16 '13 00:10

Julian Soro


1 Answers

if you want to use set_data() you need to also use set_rules for that POST/GET field.

Since you've commented out all your set_rules I can not confirm that this is the issue but most likely it is.

please check if you have this line in your code

$this->form_validation->set_rules('fname', 'First name', 'trim|required');

So if you want to re-populate field with name="fname" you need to have set_rules() // as line above for it otherwise it won't process therefore set_value('fname') is empty.

like image 198
Kyslik Avatar answered Sep 28 '22 03:09

Kyslik