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

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


Membername
Password
Password Confirm
Email Address

Setting rules using an array rule.

Controller code:-

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:-

$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



Membername
Password
Password Confirm
Email Address

Callbacks: Your own validation methods.

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.

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.

Leave a Comment