How to Create Custom Validation Rule in CodeIgniter 4 ?

Share this Article
Reading Time: 4 minutes
3,886 Views

When we create a form in any type of application like it will be an web, mobile etc. We need to validate form input fields. We can’t trust on the values entered by end users. Because sometimes we will receive valid values but 80-90% times we receive invalid too.

Inside this article we will see How to Create Custom Validation Rule in CodeIgniter 4. These are custom validation rules where we can put our logic.

We have to do few simple steps to create and then use the custom validation rule in codeigniter 4.

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.


Turn Development Mode On

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.


Register Custom Validation Rule

There are few steps we need to follow to register custom validation rules.

  • Create a folder in /app directory to store custom validation rules.
  • Create a Custom file which will contains custom validation rules (Custom added validation logics)
  • Register custom validation
  • Use Custom added validation rules

Step #1

Create a folder in /app directory to store custom validation rules.

We will create a folder with name Validation inside /app. You can choose any name.

Step #2

Create a Custom file which will contains custom validation rules (Custom added validation logics).

Let’s create a file with name CustomRules.php in /app/Validation folder.

Open CustomRules.php file and write this piece of code into it.

<?php
namespace App\Validation;

class CustomRules{

  // Rule is to validate mobile number digits
  public function mobileValidation(string $str, string $fields, array $data){
    
    /*Checking: Number must start from 5-9{Rest Numbers}*/
    if(preg_match( '/^[5-9]{1}[0-9]+/', $data['mobile'])){
      
      /*Checking: Mobile number must be of 10 digits*/
      $bool = preg_match('/^[0-9]{10}+$/', $data['mobile']);
      return $bool == 0 ? false : true; 
      
    }else{
      
      return false;
    }
  }
}

Step #3

Load custom validation to application.

Open up the file /app/Config/Validation.php. Inside this we will register custom validation. Search for $ruleSets

# Add at header
use \App\Validation\CustomRules;

public $ruleSets = [
  //... other rules
  CustomRules::class, // here we have registered
];

Step #4

Use Custom added validation rules.

Let’s say we have a form in which we have a input field for mobile number. Next, we need to add form validation rules at server where we are submitting form data.

"mobile" => "required|mobileValidation[mobile]"

Here, as we can see we have two different rules for input type mobile.

  • required is the predefined validation rule in codeigniter 4
  • mobileValidation is custom rule added what we have created above.

Here, you can find some articles over validation rules in codeigniter 4. Please also look into these to understand in a clear vision.

We hope this article helped you to learn How to Create Custom Validation Rule 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