Welcome Guest, Not a member yet? Register   Sign In
set_value() with redirect() issue
#15

[eluser]JulijanAndjelic[/eluser]
I know the topic is old, but since I ran into this same problem many times, so I decided to extend the native CI_Form_validation class.

Simply save the following code in application/libraries/MY_Form_validation.php

Code:
<?php

class MY_Form_validation extends CI_Form_validation {

function __construct() {
  parent::__construct();
  $this->CI = &get;_instance();
  $this->CI->load->library('session');
  //store the posted data as soon as loaded
  foreach ($this->CI->input->post() as $k => $v) {
   $this->CI->session->set_flashdata('data_'.$k, $v);
  }
}

function run() {
  $valid = parent::run();
  if (! $valid) {
   $OBJ =& _get_validation_object();
   foreach ($this->CI->input->post() as $k => $v) {
    $this->CI->session->set_flashdata('error_'.$k, $OBJ->error($k, '', ''));
   }
  }
  
  return $valid;
}

}

?>

then load the form validation class and do the validation just like you normally do:
Code:
$this->load->library('form_validation');
if ($this->form_validation->run() == FALSE) {
    redirect('myform');
}

What this will do is it will save all posted data and validation errors in a flashdata so it is available to you after redirect.

You can access the data using (in your views (form)):
Code:
$this->session->flashdata('data_field_name'); //replace field name with your actual field name

and to access form errors use:
Code:
$this->session->flashdata('error_field_name'); //replace field name with your actual field name

Needless too say that you must have the session class loaded so that it is available in your views.

Note: You don't need to load the form_validation library on the redirected page, all data is in flashdata!

Hope this helps someone.


Messages In This Thread
set_value() with redirect() issue - by El Forum - 05-14-2012, 05:40 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 05:45 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 05:47 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 05:51 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 06:30 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 06:31 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 06:34 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 07:19 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 08:20 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 08:41 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 08:42 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 08:50 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 09:16 AM
set_value() with redirect() issue - by El Forum - 05-14-2012, 09:19 AM
set_value() with redirect() issue - by El Forum - 02-08-2013, 12:47 PM
set_value() with redirect() issue - by El Forum - 02-08-2013, 01:29 PM
set_value() with redirect() issue - by El Forum - 02-08-2013, 02:41 PM
set_value() with redirect() issue - by El Forum - 02-08-2013, 05:30 PM



Theme © iAndrew 2016 - Forum software by © MyBB