I realise this request goes against the example provided in the CI documentation (which advises a separate 'success' page view), but I would like to reutilise a given form view after a form has been successfully submitted - displaying a success message then displaying a blank form. I've tried a few ways unsuccessfully to clear the validation set values (unsetting $_POST
, setting rules / fields to an empty array and rerunning validation).
I could redirect to the same page, but then I'd have to set a session variable to display a success message - which is a messy approach.
Any ideas how to best achieve the above?
Redirect to itself. This way, no submissions have been run... This also gives you a way to show the flash_data.
$this->load->library('form_validation');
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('surname', 'Sur Name', 'required');
if ($this->form_validation->run() === TRUE)
{
// save data
$this->session->set_flashdata('message', 'New Contact has been added');
redirect(current_url());
}
$this->load->view('contacts/add', $this->data);
Another solution, extend the library CI_Form_validation
. The property $_field_data
is protected, so we can acces it:
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
public function clear_field_data() {
$this->_field_data = array();
return $this;
}
}
And call the new method. This way, you can pass data without storing data in session.
class Item extends Controller
{
function Item()
{
parent::Controller();
}
function add()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'name', 'required');
$success = false;
if ($this->form_validation->run())
{
$success = true;
$this->form_validation->clear_field_data();
}
$this->load->view('item/add', array('success' => $success));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With