Home > Blockchain >  Codeigniter 4 - Store validation rules in separate files
Codeigniter 4 - Store validation rules in separate files

Time:11-13

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.

CodePudding user response:

Steps

  1. Create a custom directory for storing your validation rules. I.e app/Validation.

  2. Create a class under that directory for your 'User' rules. I.e: app/Validation/UserRules.php

<?php

namespace App\Validation;

class UserRules
{
    public function create()
    {
        return [
            '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]',
            ],
        ];

    }

    public function update()
    {
        return [
            // Add 'User' update rules here.
        ];
    }
}

  1. In the \Config\Validation config file, set the relevant 'User' validation rules in the constructor. I.e:
<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;
    // ...

class Validation extends BaseConfig
{
    // --------------------------------------------------------------------
    // Setup
    // --------------------------------------------------------------------
    public $userCreate = [];
    public $userUpdate = [];

    public function __construct()
    {
        $this->userCreate = ($userRules = new \App\Validation\UserRules())->create();
        $this->userUpdate = $userRules->update();

    }

    // ...
}

  1. In your Controllers, you may access validation groups as usual.

CodePudding user response:

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;
            }
        }
    }


}
  • Related