CodeIgniter 4 Create Custom Validation Rule Tutorial
Tags: codeigniter, php, programmers, webdev
Published: 2023-08-27 10:23:43

Creating personalised validation rules in your CodeIgniter 4 applications is critical for assuring data accuracy and user input integrity. By creating custom rules tailored to your project’s specific needs, you not only improve the precision of your validation process but also streamline the entire operation of your application.

CodeIgniter also provides several pre-defined rules in application. You can learn the basic concept of [form validation of CodeIgniter](https://onlinewebtutorblog.com/codeigniter-4-form-validation-library/) from here.

## 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.

## Register Custom Validation Rule

First you have to create validation file and then register.


**Example #1: Create Validation File (Mobile Validation)**

Purpose: This validation rule basically checks the pattern of mobile number, length and then validate it.

Open project terminal and run this command,

`$ php spark make:validation Mobile --suffix`

It will create a file with name **MobileValidation.php** inside /app/Validation folder. Validation folder was not available before running above command but when it generated validation file. It also created that folder as well.

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

```
<?php

namespace App\Validation;

class MobileValidation
{
    public function checkMobile($value, string &$error = null): bool
    {
        /*Checking: Number must start from 5-9{Rest Numbers}*/
        if(preg_match( '/^[5-9]{1}[0-9]+/', $value)){
        
            /*Checking: Mobile number must be of 10 digits*/
            if(!preg_match('/^[0-9]{10}+$/', $value)){

                $error = "Mobile number is not valid";
                return false;
            }
            return true;
        }else{  
            return false;
        }
    }
}

```

## Register Custom Validation Class

Load custom validation to application.

Open **Validation.php** file from /app/Config folder. Inside this you have to register your custom validation rules.

Search for **$ruleSets**

```
//...

# Add at header
use \App\Validation\MobileValidation;
use \App\Validation\NameValidation;

/**
* Stores the classes that contain the
* rules that are available.
*
* @var string[]
*/
public array $ruleSets = [
    Rules::class,
    FormatRules::class,
    FileRules::class,
    CreditCardRules::class,
    MobileValidation::class, // custom rule
    NameValidation::class, // custom rule
];

//...

```

## Concept to Use: Custom Validation Rule

Use Custom added validation rules.

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

`"mobile" => "required|checkMobile"`

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

**required** is the predefined validation rule in codeigniter 4
**checkMobile** is custom rule added to check a valid mobile number.

We hope this article helped you to learn about CodeIgniter 4 Create Custom Validation Rule Tutorial in a very detailed way.