Email Validation Rules in CodeIgniter 4

Share this Article

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. As we are in CodeIgniter 4 section, we will see Email validation rules in codeigniter 4.

In CodeIgniter 4, there are several form validation properties we have. 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.

Also we have Mobile validation step by step guide in CodeIgniter 4 Click here to go.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.

Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed. Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

Settings Environment Variables

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

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.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.

Application Routes Configuration

To configure application routes, we need to open up the file Routes.php from /app/Config. This is the main routes config file where we will do all routes of application.

//.. Other route

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

View File – Create User Form

As, we have taken fields as name, email, mobile. So with respective with these fields we need to set the user layout. Views templates will be created inside /app/Views/add-student.php

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.

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

Application Controller Settings

Controller is the functional file. Firstly let’s load a helper at Parent Controller i.e BaseController.php. This file is in /app/Controllers folder.

Search helpers in BaseController and load “url” into helpers.

protected $helpers = [‘url’];

After loading this url helper, we will able to use site_url() and base_url() in Controllers & Views else we should have some error.

We will create application controller at /app/Controllers. Let’s create Student.php inside the given folder. Open project into terminal and type this spark command.

$ php spark make:controller Student

Write the following code into /app/Controllers/Student.php


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" => "Mobile number 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 fix email validation rules in codeigniter 4 in a very detailed way.

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.

Find More on CodeIgniter 4 here

2 thoughts on “Email Validation Rules in CodeIgniter 4”

Leave a Comment