Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter form validation using session variables

How do I get the CodeIgniter form validation to validate the $_SESSION if there is no passed form data? I tried manually setting the $_REQUEST variable, but it doesn't seem to work.

i.e. I have a function search in the controller which validates the form input passed, and either returns you to the previous page with errors, or else moves you onto the next page. But I want this function to also work if you previously filled out this page, and the info is stored in the $_SESSION variable.

function search () {
    $this->load->library("form_validation");
    $this->form_validation->set_rules("flightID", "Flight Time", "required|callback_validFlightID");
    $this->form_validation->set_rules("time", "Flight Time", "required|callback_validFlightTime");

    $this->setRequest(array("flightID", "time"));

    // adding session check allows for inter-view navigation
    if ($this->form_validation->run()) {

        // some application logic here

        $this->load->view("seats", $data);
    } else {
        $this->logger->log($_REQUEST, "request");
        // redirect back to index
        $this->index();
    }   
}
function setRequest () {
    // make sure none of the parameters are set in the request
    foreach ($vars as $k) {
        if (isset($_REQUEST[$k])) {
            return;
        }   
    }   

    foreach ($vars as $k) {
        if (isset($_SESSION[$k])) {
            $_REQUEST[$k] = $_SESSION[$k];
        }   
    }   

}
like image 302
Daniel Kats Avatar asked Sep 02 '26 08:09

Daniel Kats


1 Answers

You can store the form post info in a session using the following codeigniter functions

$formdata = array(
                    'flightID' => $this->input->post('flightID'),
                    'time' => $this->input->post('time')
                );
                $this->session->set_userdata($formdata);

and the information can be retrieved with the following

$this->session->userdata('flightID')
$this->session->userdata('time')
like image 179
Philwn Avatar answered Sep 04 '26 23:09

Philwn