Search code examples
formsvalidationcodeigniterrepopulation

CodeIgniter repopulate form from both session data & form validation?


I have a form with a couple search options, like a checkbox array and radio button. By using the form validation library I have the form repopulating after a submit, like so:

echo form_checkbox('check_track[]', '1', set_checkbox('check_track[]', '1', TRUE));
echo form_dropdown('select_year', $options, set_value('select_year', '2013'), $attribs);

I also save all the form options (by storing the post) into session userdata. Is it possible to repopulate all the fields from the session data if $_SERVER['REQUEST_METHOD'] !== 'POST' but keep repopulating based on form validation otherwise?


Solution

  • I ended up just faking that a POST had happened before the form validation stuff ran to get repopulation to work:

    if(!isset($_POST['something']) && $this->session->userdata('something'))
    { 
            $_POST = $this->session->all_userdata(); 
    }
    $this->form_validation->set_rules('something', 'stuff', 'required');
    .
    .
    .