Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Controller.php Add custom Config\Validation class #8908

Closed
wants to merge 16 commits into from
22 changes: 18 additions & 4 deletions system/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class Controller
*/
protected $validator;

/**
* Config Validation file
*
* @var Validation
*/
protected $configValidation;
Copy link
Member

@kenjis kenjis Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use typed properties.

Suggested change
protected $configValidation;
protected ?Validation $configValidation = null;


/**
* Constructor.
*
Expand All @@ -81,9 +88,10 @@ class Controller
*/
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
$this->request = $request;
$this->response = $response;
$this->logger = $logger;
$this->request = $request;
$this->response = $response;
$this->logger = $logger;
$this->configValidation = config(Validation::class);
Copy link
Member

@kenjis kenjis Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better that $configValidation is nullable, and if it is not null, set Config\Validation::class in setValidator().
Because when we don't use validation in the controller, we don't need the Config\Validation instance.


if ($this->forceHTTPS > 0) {
$this->forceHTTPS($this->forceHTTPS);
Expand Down Expand Up @@ -152,16 +160,22 @@ protected function validateData(array $data, $rules, array $messages = [], ?stri
return $this->validator->run($data, null, $dbGroup);
}

protected function setConfigValidator(Validation $config): void
{
$this->configValidation = $config;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this method?
If there is a protected property, we can set it in our controllers.


/**
* @param array|string $rules
* @param array $messages An array of custom error messages
*/
private function setValidator($rules, array $messages): void
{
$this->validator = service('validation');

// If you replace the $rules array with the name of the group
if (is_string($rules)) {
$validation = config(Validation::class);
$validation = $this->configValidation;

// If the rule wasn't found in the \Config\Validation, we
// should throw an exception so the developer can find it.
Expand Down
16 changes: 16 additions & 0 deletions user_guide_src/source/incoming/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@

.. literalinclude:: controllers/006.php

.. _controller-setconfigvalidator:

$this->setConfigValidator()
=================

Check warning on line 114 in user_guide_src/source/incoming/controllers.rst

View workflow job for this annotation

GitHub Actions / Check User Guide syntax

Title underline too short.

Check warning on line 114 in user_guide_src/source/incoming/controllers.rst

View workflow job for this annotation

GitHub Actions / Check User Guide syntax

Title underline too short.

.. versionadded:: 4.x.x

If you need to validate rules that are in a personal validation file and this extension class of the ``Config\Validation`` class is extended, you can configure it as follows.
Can be useful when working with standalone modules.

It is required that you extend from the ``Config\Validation`` class.

By default, the ``Config\Validation`` class is used.

.. literalinclude:: controllers/026.php

.. _controller-validate:

$this->validate()
Expand Down
24 changes: 24 additions & 0 deletions user_guide_src/source/incoming/controllers/026.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace CustomModule\Config;

use Config\Validation;

class ModuleValidation extends Validation
{
public array $rule = [];

// ...
}

namespace App\Controllers;

use CustomModule\Config\ModuleValidation;

class Helloworld extends BaseController
{
public function index()
{
$this->setConfigValidator(config(ModuleValidation::class));
}
}
Loading