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

Bug: validation run even if required_with condition #7557

Closed
shishamo opened this issue Jun 11, 2023 · 12 comments
Closed

Bug: validation run even if required_with condition #7557

shishamo opened this issue Jun 11, 2023 · 12 comments
Assignees

Comments

@shishamo
Copy link

shishamo commented Jun 11, 2023

PHP Version

8.1

CodeIgniter4 Version

4.3.4

CodeIgniter4 Installation Method

Manual (zip or tar.gz)

Which operating systems have you tested for this bug?

macOS

Which server did you use?

apache

Database

PostgreSQL 15.2

What happened?

even if the field in the required_with condition does not exist the other validation rules run

Steps to Reproduce

$data = [];

$validation = Services::validation();
$validation = $validation->setRules([
    'testField' => 'required_with[otherField]|valid_date',
]);

$result = $validation->run($data);

Expected Output

the validation rules should not run if the field required in the required_with condition does not exist?

Anything else?

No response

@shishamo shishamo added the bug Verified issues on the current code behavior or pull requests that will fix them label Jun 11, 2023
@kenjis kenjis added bug Verified issues on the current code behavior or pull requests that will fix them and removed bug Verified issues on the current code behavior or pull requests that will fix them labels Jun 11, 2023
@kenjis
Copy link
Member

kenjis commented Jun 11, 2023

I'm not sure this is a bug or not.

When you permit empty data, add the permit_empty rule:
required_with[otherField]|permit_empty|valid_date'

@shishamo
Copy link
Author

@kenjis
in that case, testField will be accepted if the value is an empty string a null or even an empty array, even if the value is not a valid_date
i think it is a bug, isn't it?

@kenjis
Copy link
Member

kenjis commented Jun 11, 2023

Exactly. In this case, the rule set should be 'required_with[otherField]|valid_date'.

@kenjis
Copy link
Member

kenjis commented Jun 12, 2023

Please check #7562

@michalsn
Copy link
Member

I have to say I don't really understand the scenario OP is trying to solve here...
required_with[otherField]|permit_empty|valid_date should work just fine.

@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Jun 13, 2023
@kenjis
Copy link
Member

kenjis commented Jun 13, 2023

The true issue is the following test cases pass. See #7562 (comment)
But it seems difficult to handle them with the current required_with and other rules.

    /**
     * @see https://github.com/codeigniter4/CodeIgniter4/issues/7557
     *
     * @dataProvider RequiredWithAndOtherRuleProvider
     */
    public function testRequiredWithAndOtherRule(bool $expected, array $data): void
    {
        $this->validation->setRules([
            'mustBeADate' => 'required_with[otherField]|permit_empty|valid_date',
        ]);

        $result = $this->validation->run($data);

        $this->assertSame($expected, $result);
    }

    public function RequiredWithAndOtherRuleProvider(): Generator
    {
        yield from [
            [true, ['mustBeADate' => '']],
            [true, ['mustBeADate' => null]],
            [true, ['mustBeADate' => []]],
        ];
    }

@michalsn
Copy link
Member

Seems like we would need something like permit_not_exists rule for this. Since permit_empty is too wide.

@kenjis
Copy link
Member

kenjis commented Jun 13, 2023

Yes, permit_empty is too wide.
Ref #3670

@michalsn
Copy link
Member

@shishamo I forgot we already have if_exist rule... If you're sending the mustBeADate field only when otherField is checked, then you may give it a try: required_with[otherField]|if_exist|valid_date

@kenjis
Copy link
Member

kenjis commented Jun 13, 2023

if_exist is also a special rule. it does nothing when the key does not exist in the data.
So required_with[otherField] does not process if the key does not exist.

@michalsn
Copy link
Member

Yes, you're right.

@kenjis
Copy link
Member

kenjis commented Jun 13, 2023

This is not a bug.

How it works:

required_with[otherField]|permit_empty|valid_date

  1. When otherField exists, it means required|permit_empty|valid_date
    • required has higher priority than permit_empty, so it is required|valid_date.
    • empty is not permitted.
  2. When otherField does not exist, it means permit_empty|valid_date
    • empty is permitted.

required_with[otherField]|valid_date

  1. When otherField exists, it means required|valid_date
    • empty is not permitted.
  2. When otherField does not exist, it means valid_date
    • empty is not permitted.

Note: In general, required_with[otherField]|valid_date is redundant. We don't need required_with (or required), because the valid_date rule causes the value to be required.

@kenjis kenjis closed this as completed Jun 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants