[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.