Search code examples
validationcodeigniter-4

Codeigniter 4 - Store validation rules in separate files


I have all my rules in the Validation config file, like the documentation suggest: https://codeigniter4.github.io/userguide/libraries/validation.html#saving-sets-of-validation-rules-to-the-config-file

For example:

public $userCreate = [
    'first_name' => [
        'label' => 'First Name',
        'rules' => 'required|string|max_length[60]'
    ],
    'last_name' => [
        'label' => 'Last Name',
        'rules' => 'required|string|max_length[60]',
    ],
    'email' => [
        'label' => 'Auth.email',
        'rules' => 'required|max_length[254]|valid_email|is_unique[users.email]',
    ],
];

In my controllers I can access my validation groups like this:

$validation = \Config\Services::validation();
$rules = $validation->getRuleGroup('userCreate');

As my app gets bigger, I need more and more validation rules, so the question is, is there a way to organize them in separate files and not to have all of them in a single config file? Something like the custom rules, which are loaded in the config file and stored separately.


Solution

  • Thanks to @steven7mwesigwa I came up with a solution that suits me the most.

    First I created separate classes inside the App/Validation folder. For example these 2 classes:

    App\Validation\Auth.php

    <?php
    
    namespace App\Validation;
    
    class Auth {
    
        public $login = [
            'email' => [
                'label' => 'E-mail',
                'rules' => 'required|max_length[254]|valid_email',
            ],
            'password' => [
                'label' => 'Password',
                'rules' => 'required',
            ],
            'remember' => [
                'label' => 'Remember me',
                'rules' => 'if_exist|permit_empty|integer',
            ]
        ];
    
    }
    

    App\Validation\User.php

    <?php
    
    namespace App\Validation;
    
    class User {
    
        public $userCreate = [
            'first_name' => [
                'label' => 'First Name',
                'rules' => 'required|string|max_length[60]',
            ],
            'last_name' => [
                'label' => 'Last Name',
                'rules' => 'required|string|max_length[60]',
            ],
            'email' => [
                'label' => 'E-mail',
                'rules' => 'required|max_length[254]|valid_email|is_unique[users.email]',
            ],
        ];
    
    }
    

    The next step is to add a construct method to the existing validation config file:

    App\Config\Validation.php

    <?php
    
    namespace Config;
    
    use CodeIgniter\Config\BaseConfig;
    
    class Validation extends BaseConfig {
    
        ...
    
        // --------------------------------------------------------------------
        // Rules
        // --------------------------------------------------------------------
    
        public function __construct() {
            $ruleGroups = [
                new \App\Validation\Auth(),
                new \App\Validation\User(),
            ];
    
            foreach ($ruleGroups as $ruleGroupClass) {
                foreach ((array) $ruleGroupClass as $groupName => $rules) {
                    $this->{$groupName} = $rules;
                }
            }
        }
    
    
    }