I can't seem to reload the form to see the error_messages. I have one function called index() and id() this is the method action which get's executed when the form button is pressed. Also I am using Datampper with it. I know it has form validation itself but I prefer codeigniter's form validations.
public function save_comments($article_id)
{
// Create object for comments
$comments = new Comment_model();
// Form Inputs & DataMapper Validaiton
$comments->name = $this->input->post('name');
$comments->body = $this->input->post('body');
$comments->article_id = $article_id;
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('body', 'Message', 'required');
if ($this->form_validation->run() == FALSE)
{
//Something to reaload the form ?
}
else
{
$comments->save();
redirect('article/id/'. $article_id);
}
}
Blockquote
You should not need to reload the page to show the errors, why not do something like:
public function save_comments($article_id)
{
// Create object for comments
$comments = new Comment_model();
// Form Inputs & DataMapper Validaiton
$comments->name = $this->input->post('name');
$comments->body = $this->input->post('body');
$comments->article_id = $article_id;
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('body', 'Message', 'required');
// Check the form has been submitted
if ($this->input->post('my_submit_button') !== FALSE)
{
if ($this->form_validation->run() !== FALSE)
{
// Validated OK, so save
$comments->save();
redirect('article/id/'. $article_id);
return;
}
}
$this->load->view('my_form');
}
So essentially the form form is always shown. If you submit your form, you check the validation, if it is valid, go ahead and save, then return
to stop the rest of the function executing, else let the function continue to exectute and show the form again.
Alternatively, just use the following to direct to the current URL:
redirect(current_url(), 'refresh');