-
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
Conversation
I think it is interesting to be able to add the custom configuration class, when you are using modules for example
Thank you for sending this PR! We expect the following in all Pull Requests (PRs).
Important We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works. If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work on the framework than you do. Please make it as painless for your contributions to be included as possible. See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md |
What do you mean? |
By working in a modular way, each module is independent, and instead of adding validations in Config\Validation in each module I have a Validation class that extends Config\Validation. Ex: class ApiValidation extends Validation Loading my custom class validation: Config(ApiValidation::class) |
This is a breaking change. |
@daycry Thank you for the explanation. Indeed, the use case is likely. |
What do I need to do? |
See #8908 (comment)
But I'm not sure this PR (this implementation) should be merged. |
We have been recommending using We need to see what other people think about this breaking change. |
Yes, BREAKING CHANGE should be avoided as much as possible. |
How about adding a property for the validation classname? |
Maybe creating a public method allowing set the new attribute Validation like this. Private Validation $config; Public method setValidation(Validation $config){ Replacing config('Validation') with this attribute. Maybe is better. |
I've done some changes for delete breaking change tag |
system/Controller.php
Outdated
protected function setConfigValidator(Validation $config): void | ||
{ | ||
$this->configValidation = $config; | ||
} |
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.
Do we really need this method?
If there is a protected property, we can set it in our controllers.
$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 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.
system/Controller.php
Outdated
* | ||
* @var Validation | ||
*/ | ||
protected $configValidation; |
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.
protected $configValidation; | |
protected ?Validation $configValidation = null; |
Maybe, this method isn't necessary because It can set in every controllers using $this->configValidator = config... |
Why do you need a new config? Just to add rules? There is a better solution and it does not depend on the system.
Example app/Validation/RuleGroups/UserRules.php: <?php
declare(strict_types=1);
namespace App\Validation\RuleGroups;
class User
{
/**
* Login form
*/
public static function signin(): array
{
return [
'email' => [
'label' => 'User.email',
'rules' => 'required|valid_email',
],
'password' => [
'label' => 'User.password',
'rules' => 'required|min_length[6]',
],
];
}
} You can apply any rules separately from the system, have separate files for each action (instead of 100 properties in Validation). All you need is to add new Validation types |
Thank you! I didn't know this. |
And an important advantage: you can execute any code in the method. This is not always possible for a class property. |
An example, please? I always use custom rules for check complexes data object or something like this. |
Hmm, array variants: |
Aaahhh ok ok I do this thinks some times. Thank you for your help! |
The feature to set validation rules in Instantiating a Config class that extends Also, when using the feature, we specify the rule as a string (property name), so we cannot jump to the rule definition on an IDE. If we use a simple static class that returns a rule array, it does not check unnecessary Environment Variables and we can jump to the method on an IDE. And we can put the class in anywhere. |
I checked the code of CI and now is very flexible check data, I like It! I Will close this PR. Thank you for your help! |
I think it is interesting to be able to add the custom configuration class, when you are using modules for example
Description
Explain what you have changed, and why.
Checklist: