Welcome Guest, Not a member yet? Register   Sign In
Controller's variable $validator: wrong Validation class?

(This post was last modified: 10-13-2019, 02:37 AM by Ekley.)

Hello  Smile

I searched for a similar topic but I didn't find any... so I hope it won't be redundant. I have an "issue" (or, at least, a comprehension issue) with the initialization of the variable $validator in the CodeIgniter\Controller class.

Let me explain:
- I extended the CodeIgniter\Validation\Validation class with additional methods:
PHP Code:
<?php namespace App\Libraries;

CodeIgniter\Validation\Validation as CoreValidation;

Validation extends CoreValidation

    public function __construct(\Config\Validation $config, \CodeIgniter\View\RendererInterface $view)

    /** ... my custom code here **/



- I rewrote the validation() method in the Config\Services class to instanciate my custom class (as explained in the user guide):
PHP Code:
public static function validation(\Config\Validation $config nullbool $getShared true)
        if ($getShared)
            return static::getSharedInstance('validation'$config);

        if (is_null($config))
            $config config('Validation');

        return new \App\Libraries\Validation($config, static::renderer());

- in my custom controller, I use the method validate() from the class CodeIgniter\Controller. For information, this method initializes the variable $validator:
PHP Code:
protected function validate($rules, array $messages = []): bool
$this->validator Services::validation();

// If you replace the $rules array with the name of the group
if (is_string($rules))
$validation = new \Config\Validation();

// If the rule wasn't found in the \Config\Validation, we
            // should throw an exception so the developer can find it.
if (! isset($validation->$rules))

// If no error message is defined, use the error message in the Config\Validation file
if (! $messages)
$errorName $rules '_errors';
$messages  $validation->$errorName ?? [];

$rules $validation->$rules;

$success $this->validator


What I expect: the variable $validator is an instance of my custom Validation class.

What I get: the variable is an instance of CodeIgniter\Config\Services class.

Knowing the philosophy of the framework and the extensibility it provides, it seems to be bug. If not, could it be clearly stated when CodeIgniter\Config\Services is preferred to App\Config\Services, please? At least to know when we can rely on our custom extensions of CI.

If it is done on purpose, obviously, I'll rewrite the validate method in my custom Controller.

Regards and thank you!

Theme © iAndrew 2016 - Forum software by © MyBB