How to Create Custom Validation Rule in CodeIgniter 4?

Share this Article
Reading Time: 4 minutes
14,911 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.

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.


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.

Buy Me a Coffee

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.