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 rule with * gets incorrect values as dot array syntax #8128

Closed
kenjis opened this issue Oct 30, 2023 · 2 comments · Fixed by #8129
Closed

Bug: Validation rule with * gets incorrect values as dot array syntax #8128

kenjis opened this issue Oct 30, 2023 · 2 comments · Fixed by #8129
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@kenjis
Copy link
Member

kenjis commented Oct 30, 2023

From #8079 (comment)

The name key is contacts.just.friends.*.name, not contacts.*.name.

    public function index(): string
    {
        // Extend the user guide case and add one more layer.
        $requestData = [
            'contacts' => [
                'name' => 'Joe Smith',
                'just' => [
                    'friends' => [
                        [
                            'name' => 'Fred Flinstone',
                        ],
                        [
                            'name' => 'Wilma',
                        ],
                    ],
                ],
            ],
        ];

        $this->validator = \Config\Services::validation();
        $this->validator->setRules([
            'contacts.*.name' => 'required|max_length[1]',
        ]);

        dd(
            $this->validator->run($requestData),
            $this->validator->getErrors(),
            $this->validator->getValidated()
        );
    }
$this->validator->run(...) boolean false
⧉⌕$this->validator->getErrors() array (2)
    ⇄contacts.just.friends.0.name => string (63) "The contacts.*.name field cannot exceed 1 characters in length."
    ⇄contacts.just.friends.1.name => string (63) "The contacts.*.name field cannot exceed 1 characters in length."
$this->validator->getValidated() array (0)
⧉ Called from .../app/Controllers/Home.php:34 [dd()]
@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Oct 30, 2023
@kenjis
Copy link
Member Author

kenjis commented Oct 30, 2023

There is the bug.

if (strpos($field, '*') !== false) {
$values = array_filter(array_flatten_with_dots($data), static fn ($key) => preg_match(
'/^'
. str_replace(['\.\*', '\*\.'], ['\..+', '.+\.'], preg_quote($field, '/'))
. '$/',
$key
), ARRAY_FILTER_USE_KEY);

$values = {string[2]} ["Fred Flinstone", "Wilma"]
 contacts.just.friends.0.name = "Fred Flinstone"
 contacts.just.friends.1.name = "Wilma"

This code came from: d6a2327f and 7dce1aa7d58

@kenjis
Copy link
Member Author

kenjis commented Oct 30, 2023

            if (strpos($field, '*') !== false) {
                $flatten = array_flatten_with_dots($data);
                $pattern ='/^'
                    . str_replace(['\.\*', '\*\.'], ['\..+', '.+\.'], preg_quote($field, '/'))
                    . '$/';

                $values = array_filter($flatten, static fn ($key) => preg_match(
                    $pattern,
                    $key
                ), ARRAY_FILTER_USE_KEY);

                // if keys not found
                $values = $values ?: [$field => null];
            } else {
$flatten = {array[4]} 
 contacts.name = "Joe Smith"
 contacts.just.friends.0.name = "Fred Flinstone"
 contacts.just.friends.1.name = "Wilma"
 DBGroup = null
$pattern = /^contacts\..+\.name$/
$values = {string[2]} ["Fred Flinstone", "Wilma"]
 contacts.just.friends.0.name = "Fred Flinstone"
 contacts.just.friends.1.name = "Wilma"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant