I am validating a form in codeigniter. If the user did not fill in the form correctly, then the page should not redirect to any other page, but display errors on each input field. Here is my code of form:
<?php echo $this->session->flashdata('errors');?>
<form action="<?php echo base_url() ?>detail/travel" method="post">
<input type="text" name="departure">
<input type="text" name="destination">
<input type="text" name="name">
<input type="text" name="cell">
</form>
and here is my method code:
function search_travel(){
if($_POST){
$config = array(
array(
'field'=>'departure',
'label'=>'departure',
'rules'=>'trim|required|alpha'
),
array(
'field'=>'destination',
'label'=>'destination',
'rules'=>'trim|required|alpha'
),
array(
'field'=>'name',
'label'=>'name',
'rules'=>'trim|required|alpha'
),
array(
'field'=>'cell',
'label'=>'cell no',
'rules'=>'trim|required'
)
);
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
$this->form_validation->set_rules($config);
if($this->form_validation->run() == FALSE){
$this->session->set_flashdata('errors', validation_errors());
redirect(base_url());
}
}
}
The problem is that by using set_flashdata I can't use form_error function and set_value function. Is there any other solution to this problem???
Under your input place form_error() you need to load the form helper though
You can autoload libraries etc config/autoload.php
$autoload['helper'] = array('form', 'html', 'url');
Form Error Example
<input type="text" name="departure">
<?php echo form_error('departure', '<div class="error">', '</div>'); ?>
<input type="text" name="destination">
<?php echo form_error('destination', '<div class="error">', '</div>'); ?>
<input type="text" name="name">
<?php echo form_error('name', '<div class="error">', '</div>'); ?>
<input type="text" name="cell">
<?php echo form_error('cell', '<div class="error">', '</div>'); ?>
As shown here https://www.codeigniter.com/userguide3/libraries/form_validation.html#changing-the-error-delimiters
Controller filename Detail.php first letter only must be upper case class and file name
<?php
class Detail extends CI_Controller {
// You can autoload helpers and libraries etc if you need to,
public function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->library('form_validation');
}
public function index() {
$this->load->view('your_form_view');
}
function search_travel() {
$this->form_validation->set_rules('departure', 'departure', 'trim|required');
$this->form_validation->set_rules('destination', 'destination', 'trim|required');
$this->form_validation->set_rules('name', 'name', 'trim|required');
$this->form_validation->set_rules('cell', 'cell', 'trim|required');
if ($this->form_validation->run() == FALSE) {
// Error
$this->load->view('your_form_view');
} else {
// Success
redirect(base_url('another/controller'));
}
}
}
View
<?php echo form_open('detail/search_travel');?>
<input type="text" name="departure">
<?php echo form_error('departure', '<div class="error">', '</div>'); ?>
<input type="text" name="destination">
<?php echo form_error('destination', '<div class="error">', '</div>'); ?>
<input type="text" name="name">
<?php echo form_error('name', '<div class="error">', '</div>'); ?>
<input type="text" name="cell">
<?php echo form_error('cell', '<div class="error">', '</div>'); ?>
// Submit button
<?php echo form_close();?>
You should avoid redirecting after failed validation. You have two options for getting back the validation error:
Load the page:
$this->load->view('page name);
you can load the view of page form specific function. example like if you have index function to load the form, then you can do this.
$this->index();
and to populate the validation errors separately you can show them like this:
<?= form_error('your input field name') ?>
or showing all the error at top of the form You can do like this.
<?php echo validation_errors(); ?>
Hope this will help.