-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from 12 commits
8c4cf1e
86689eb
b765877
15c4683
fe05abf
bf7524f
4af21cf
96c5ba2
ba0430a
ca421d0
aa5765a
7373125
ae7fe45
ba5ba80
0363d90
35a95b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,13 @@ class Controller | |
*/ | ||
protected $validator; | ||
|
||
/** | ||
* Config Validation file | ||
* | ||
* @var Validation | ||
*/ | ||
protected $configValidation; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better that |
||
|
||
if ($this->forceHTTPS > 0) { | ||
$this->forceHTTPS($this->forceHTTPS); | ||
|
@@ -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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need this method? |
||
|
||
/** | ||
* @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. | ||
|
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)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use typed properties.