-
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
Validation: permit_empty, optional arguments #3670
Comments
I like the concept but it feels a bit weird to be a parameter to
|
I think adding optional parameters to the With optional parameters, both - implementation and rules behind it would be easier to understand for users. The alternative is to make this one exceptional rule, which will be executed alongside the A separate rule might also cause some problems. What if I would like to validate the "type" only when there is a value (not empty) in the field? We would need yet another rule for the same thing? |
Would love to see that all rules support empty values (or only get called if there is a value, but this wouldn't allow to build a See #3025 |
Should i submit a pr? Would be a small fix 😇 |
@tada5hi Feel free to send a PR. |
This was closed but I don't think the issue was ever addressed. I ran into this probably again today - namely, that we don't have any way to validate nullable database fields. Consider the following: class WidgetModel extends Model
{
protected $table = 'widgets';
protected $returnType = Widget::class;
protected $allowedFields = ['name', 'cost'];
protected $validationRules = [
'name' => 'required',
'cost' => 'permit_empty|is_natural_no_zero',
];
} Since // WidgetController.php
$data = $this->request->getPost(); // ['name' => 'My Widget', 'cost' => '']
if ($this->model->insert($data)) {
... ... actually causes a database error when it tries inserting
There are a few different ways we could implement this, but being able to define a validation for |
Something like these?
|
My comment was about the ability to create a rule something like I don't know if that is still the case in |
@element-code Sorry, I don't get what you want. |
I think what @element-code is suggesting is a different way of handling this, by making a new rule. I had imagined this as an option like this:
Which basically expands any following rules to be '{type}|null` just like the database would accept. None of these solve the example problem that submitting a form with For the original issue, I like the proposal that has been floating around of adding parameters to |
Oh,
'-0' , -0 , '0.0' , -0.0 , '-0.0' also pass permit_empty .(If the rule is only permit_empty , anything passes.)
Then |
I don't feel the need to make a special case for |
The test code is broken. all zeros do not pass |
🤦♂️ What a mess. Does this mean we have never allowed zero of any kind? Now we have to decide if we break people relying on this to match the User Guide or make this weird exception. |
Or do we make the default parameters equivalent to your original suggestion: |
See #6175 and CodeIgniter4/system/Validation/Validation.php Lines 243 to 244 in 7666380
|
@MGatner By the way, where did you get the idea that |
I thought I read that in the User Guide, but that is not the case:
Must have been from the tests? Which we know now are irrelevant. This is actually good, that |
Why do you need to permit zero? Any use case? |
"empty" is a bit of a code word in PHP. While I think the method with that name is a terrible design it sets some expectations: https://3v4l.org/BSN3G If we don't include |
Some nice precedence from PHPStan on "non-empty" strings:
|
Bring up the subject. I came across this task, but initially on validation in the controller. public function setDateBirth($dateBirth): self
{
if (empty($dateBirth)) {
$dateBirth = null;
}
$this->dateBirth = DatetimeCast::get($dateBirth);
$this->attributes['date_birth'] = is_null($dateBirth) ? null : (string)$this->dateBirth;
return $this;
} To avoid misunderstandings of the validator, I intentionally delete empty fields, which is guaranteed to process if (empty($input['dateBirth'])) {
unset($input['dateBirth']);
} Otherwise, the workaround is the BeforeUpdate, before Insert, etc model events to set the desired values. |
Thanks for your feedback and solution @neznaika0 ! These days I am less inclined to solve all possible scenarios in the framework and expect developers to do some of their own handling, like you are here. |
Hi,
i love the idea to specify that a given attribute is allowed to be empty by rule, but it would be great if you can specify an explicit type, or a subset.
If the target data fileld can be an empty array, null, empty string there will be a database error for sure, if you try to insert an array for example in a boolean nullable column.
Greetings
Tada5hi
The text was updated successfully, but these errors were encountered: