How To Work with Email Validation Rules in CodeIgniter 4

Reading Time: 5 minutes

Inside this article we will see the concept i.e How To Work with Email Validation Rules in CodeIgniter 4. Article contains the classified information about Email validation in PHP Codeigniter.

Inside this we will cover validations like required, is valid email, is unique, etc. In Form submission validation is one of the major functionality we need to attach. Validation is either from client side and/or from server side as well.

In CodeIgniter 4, there are several form validation properties available. So here, we will see about all email related validations like required, a valid email format and also we will see already existence of email address in database.

Learn More –

Let’s get started.

CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
CI_ENVIRONMENT = development

Now application is in development mode.

Add Route

Open Routes.php from /app/Config folder. Add this route into it.


$routes->match(["get", "post"], "add-student", "Student::addStudent");


Create View Layout

Create add-student.php inside /app/Views folder.

Inside this view file, at top header we have used session() service to collect session temporary data to show flash message.

Flash message we will send from controller in keys of success and error.

Open add-student.php and write this complete code into it.

  // To print error messages
    print_r($validation->listErrors() );


<form action="<?= base_url('add-student') ?>" method="post">
       Name: <input type="text" name="name" placeholder="Enter name"/>

       Email: <input type="email" name="email" placeholder="Enter email"/>

       Mobile: <input type="text" name="mobile" placeholder="Enter mobile"/>

     <button type="submit">Submit</button>
  • We will submit this form to controller. At controller end we will add form validation rules.
  • print_r($validation->listErrors() ); It prints all errors at once. But in case if we want single error per line

To Check any input field has any error or not

     Email : <input type="email" name="email" class="<?= ($validation->hasError('email')) ? 'is-error' : '' ?>"/>  

Here, inside above code we are checking the field error, it exists then we are adding a class into input field with the email field of is-error.

Print any specific field error

if ($validation->hasError('email')){
    echo $validation->getError('email'); // $validation is sent from controller

Create Controller

Loading url Helper

Open BaseController.php from /app/Controllers folder.

Search for $helpers, inside this helpers array simply add this url helper.

protected $helpers = ["url"];

This url helper will load base_url() and site_url() functions.

$ php spark make:controller Student

It will create Student.php inside /app/Controllers folder. Open file and write this complete code into it.


namespace App\Controllers;

use App\Models\StudentModel;

class Student extends BaseController
	public function addStudent(){

		if($this->request->getMethod() == "post"){

			$rules = [
				"name" => "required",
				"email" => "required|valid_email|is_unique[]|min_length[6]",
				"mobile" => "required",
			$messages = [
				"name" => [
					"required" => "Name is required"
				"email" => [
					"required" => "Email required",
                    "valid_email" => "Email address is not in format",
                    "is_unique" => "Email address already exists"
                "mobile" => [
					"required" => "Mobile Number is required"
			if (!$this->validate($rules, $messages)) {

                return view("add-student", [
                    "validation" => $this->validator,
				// rest code to save data into database
		return view("add-student");
  • if ($this->request->getMethod() == “post”) Checking request type
  • $rules = []; Define form validation rules
  • $messages = [] Define custom messages
  • if (!$this->validate($rules, $messages)) {} Validating add student form
  • “email” => “required|valid_email|is_unique[]|min_length[6]” All email validation rules. required It checks email should be required, valid_email Email address pattern should be valid pattern, is_unique Email address should be unique in database, min_length Minimum length of email address should be not less than 6 characters.

More about validations over different different input fields, you can visit official document of validations of CodeIgniter 4.

We hope this article helped you to learn about How To Work with Email Validation Rules in CodeIgniter 4 in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more

2 thoughts on “How To Work with Email Validation Rules in CodeIgniter 4”

  1. These are in fact enormous ideas in about blogging. You
    have touched some good factors here. Any way keep up wrinting.

Comments are closed.