Skip to content

Bug: Validation passes if key does not exist when using asterisk #8006

@puwnd

Description

@puwnd

PHP Version

8.1

CodeIgniter4 Version

4.4.1 OR 4.3.5

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

No response

What happened?

If I use the asterisk in the validation rule, it does not check for non existing key.
There are two workarounds, but I guess they are not the best solutions.

  1. Add a default value with the given key
  2. Use with numbers instead of asterisk

Steps to Reproduce

In any controller I use the following snippet in the initController method.

        <?php
        $requestData = [
            'contacts' => [
                'friends' => [
                    ['name' => 'Fred Flinstone', 'age' => 20],
                    ['age' => 21], // 'name' key does not exist
                ]
            ]
        ];

        // $requestData['contacts']['friends'][1]['name'] ??= null; // Workaround for non existing keys

        $this->validator = Services::validation();
        $this->validator->setRules(
            [
                'contacts.friends.*.name' => 'required', // The "*" does not check for non existing keys and pass validation
                // 'contacts.friends.0.name' => 'required', // With this solution works fine even if the key does not exist
                // 'contacts.friends.1.name' => 'required', // With this solution works fine even if the key does not exist
            ]
        );

        dd($this->validator->run($requestData), $this->validator->getErrors());

Expected Output

I think the validation should check for it and fail if key does not exist (if there is a rule for it). So in my case the output should be:

$this->validator->run(...) boolean false
$this->validator->getErrors() array (1)
contacts.friends.1.name => string (46) "The contacts.friends.*.name field is required."

Now I get this:
$this->validator->run(...) boolean true
$this->validator->getErrors() array (0)

Anything else?

I did not see any reference in the documentation: https://codeigniter.com/user_guide/libraries/validation.html#setting-rules-for-array-data
I did not find any issue like this (maybe in the closed issues).

Activity

added
bugVerified issues on the current code behavior or pull requests that will fix them
on Oct 4, 2023
kenjis

kenjis commented on Oct 5, 2023

@kenjis
Member

Thank you for reporting. I've confirmed the behavior.
Yes, it seems the Validation class validates only the existing key field.

ping-yee

ping-yee commented on Oct 5, 2023

@ping-yee
Contributor

@kenjis You can assign me to handle this bug.

kenjis

kenjis commented on Oct 5, 2023

@kenjis
Member

@ping-yee Assigned.
The current code only performs processing on existing data. If the key is not present, it will not be processed. So it seems to me that we need to add a key that does not exist.

ping-yee

ping-yee commented on Oct 5, 2023

@ping-yee
Contributor

Yah, I just figure out how the current code works.

So it seems to me that we need to add a key that does not exist.

I will follow this way to fix this bug.

kenjis

kenjis commented on Oct 22, 2023

@kenjis
Member

@ping-yee Any progress?

ping-yee

ping-yee commented on Oct 23, 2023

@ping-yee
Contributor

@kenjis Yah, but this is not very optimistic and I need your help.
Or I send a draft PR first.

kenjis

kenjis commented on Oct 23, 2023

@kenjis
Member

I think this is very difficult problem. So it requires more brains.

ping-yee

ping-yee commented on Oct 23, 2023

@ping-yee
Contributor

I think this is very difficult problem. So it requires more brains.

Yes, I agree with this and I send the draft PR.

kenjis

kenjis commented on Apr 26, 2024

@kenjis
Member

As a workaround, you can use field_exists since v4.5.0.

crustamet

crustamet commented on Jul 8, 2024

@crustamet
Contributor

i can confirm that if using with no asterisks the validation still not works

using data like this

		$requestData = [
			'contacts' => [
				'friend' => 'test'
			]
		];

		$this->validator = Services::validation();
		$this->validator->setRules(
			[
				'contacts.friend' => 'required',
			]
		);

		dd($this->validator->run($requestData), $this->validator->getErrors());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugVerified issues on the current code behavior or pull requests that will fix them

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @kenjis@crustamet@puwnd@ping-yee

      Issue actions

        Bug: Validation passes if key does not exist when using asterisk · Issue #8006 · codeigniter4/CodeIgniter4