codeigniter form validation callback multiple fields

Today, We want to share with you codeigniter form validation.In this post we will show you codeigniter 4 form validation, hear for codeigniter form validation custom error message we will give you demo and example for implement.In this post, we will learn about Registration Form Validation Using Ajax In Codeigniter with an example.

Codeigniter Form Validation Library

How to load form validation library:

$this->load->library('form_validation');

Setting validation rules

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class form_check_controller extends CI_Controller
{
public function check(){
$this->load->view('validation_view');
}
public function index(){
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('membername', 'Membername', 'required');
$this->form_validation->set_rules('password', 'Password', 'required',
   array('required' => 'You must provide a %s.'));
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
 if ($this->form_validation->run() == FALSE)
    {
	$errors = validation_errors();
       $this->session->set_flashdata('error', $errors);
       redirect(base_url('index.php/form_check_controller/check'));	
}else{
    echo "successfully inserted";					
       }}}
?>

Views code

<?php echo form_open('index.php/form_check_controller/index'); ?>
<h5>Membername</h5>
<input type="text" name="membername" value="" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>

Setting rules using an array rule.

Controller code:-

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class form_check_controller extends CI_Controller
{
	public function check()
	{
		$this->load->view('validation_view');
	}
public function arraycheck()
		{
			$this->load->helper('form');
            $this->load->library('form_validation');
			$array = array(array('field' => 'membername',
                'label' => 'Membername',
                'rules' => 'required'),
				array('field' => 'password',
                'label' => 'Password',
                'rules' => 'required',
                'errors' => array('required' => 'You must provide a %s.',
                ),),
		array(
                'field' => 'passconf',
                'label' => 'Password Confirmation',
                'rules' => 'required'),
		array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required'
				));
		$this->form_validation->set_rules($array);
		if ($this->form_validation->run() == FALSE)
                {
		$errors = validation_errors();
	        $this->session->set_flashdata('error', $errors);    
		redirect(base_url('index.php/form_check_controller/check'));	
                }
                else
                {
		echo "successfully inserted";					
                }}}
?>

Prepping data

Controller code:-

Read Also:  Create Dynamic Web Pages using PHP-MYSQL

$this->form_validation->set_rules('membername', 'Membername', 'trim|required|min_length[6]|max_length[15]');
            $this->form_validation->set_rules('password', 'Password', 'required',
                        array('required' => 'You must provide a %s.')
                );
            $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
            $this->form_validation->set_rules('email', 'Email', 'required');

Re-populating the form

<?php echo form_open('index.php/form_check_controller/arraycheck'); ?>

<h5>Membername</h5>
<input type="text" name="membername" value="<?php echo set_value('membername');?>" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="<?php echo set_value('password');?>" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="<?php echo set_value('passconf');?>" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="<?php echo set_value('email'); ?>"size="50" />

<div><input type="submit" value="Submit" /></div>

</form>

Callbacks: Your own validation methods.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class form_check_controller extends CI_Controller
{
public function check()
{
$this->load->view('validation_view');
}
public function index()
{
$this->load->helper('form');
 $this->load->library('form_validation');
$this->form_validation->set_rules('membername', 'Membername', 'callback_membername_check');
$this->form_validation->set_rules('password', 'Password', 'required',
array('required' => 'You must provide a %s.'));
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
$this->form_validation->set_rules('email', 'Email','required');
$check = $this->form_validation->run();
if ($check == FALSE)
{
$errors = validation_errors();
$this->session->set_flashdata('error', $errors);
redirect(base_url('index.php/form_check_controller/check'));	
}
else
{
echo "successfully inserted";					
}}

public function membername_check($check)
{
if ($check == '')
{
$this->form_validation->set_message('membername_check', 'The {membername} field can not be the word "null"');
return FALSE;
}
else
{
return TRUE;
}}}
?>

Setting error messages.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class form_check_controller extends CI_Controller
{
public function check()
{
$this->load->view('validation_view');
}
public function index()
 {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_message('min_length', '{field} must have at least {param} characters.');
$this->form_validation->set_rules('membername', 'Membername', 'callback_membername_check');
$this->form_validation->set_rules('password', 'Password', 'rule1|rule2|rule3',
array('rule2' => 'Error Message on rule2 for this field_name'));
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
$this->form_validation->set_rules('email', 'Email','required');
$check = $this->form_validation->run();
if ($check == FALSE)
{
$errors = validation_errors();
$this->session->set_flashdata('error', $errors);
redirect(base_url('index.php/form_check_controller/check'));	
}
 else
{
echo "successfully inserted";					
}}}
?>

I hope you get an idea about codeigniter form validation error message.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.