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

[9.x] Added support for index and position placeholders in array validation messages #41123

Merged
merged 10 commits into from
Feb 24, 2022

Conversation

Bird87ZA
Copy link
Contributor

Adds a backward compatible change that enables a developer to use :index or :pos in the message for array inputs. Assumes 0 or 1 respectively if it's not an array input.

Discussed in #32977

@Bird87ZA
Copy link
Contributor Author

Not sure why the builds are failing. Like I can read why, but can't see if and what I should change to fix it.

@GrahamCampbell GrahamCampbell changed the title Added support for index and position placeholders in array validation messages [9.x] Added support for index and position placeholders in array validation messages Feb 19, 2022
@Bird87ZA
Copy link
Contributor Author

@gguney both changes are made. Thank you. Any idea why the PHP testing fails?

@Bird87ZA
Copy link
Contributor Author

Seems to be all passing now so no issues there

@taylorotwell
Copy link
Member

What if the item is more deeply nested? foo.*.bar.*.baz, etc.?

@Bird87ZA
Copy link
Contributor Author

Perhaps something like this?

    // $message would be something like 'Item :key-index is required'

    protected function replaceIndexPlaceholder($message, $attribute)
    {
        $attribute = explode('.', $attribute);

        if (count($attribute) > 1) {
            $message = str_ireplace(sprintf(':%s-index', $attribute[0]), $attribute[1] ?? 0, $message);
            unset($attribute[0], $attribute[1]);
            if (count($attribute) > 1) {
                return $this->replaceIndexPlaceholder($message, implode('.', $attribute));
            } else {
                return $message;
            }
        } else {
            return str_ireplace(':index',  0, $message);
        }
    }

I'm all ears if you have a different approach.

@ionutantohi
Copy link

This has been attempted before in #35754. You might want to check it out.

@Bird87ZA
Copy link
Contributor Author

This has been attempted before in #35754. You might want to check it out.

Thanks, will have a read through it and see what I can come up with.

@taylorotwell
Copy link
Member

I've updated this so that more deeply nested indexes / positions can be accessed via second-index, third-index, fourth-index, etc.

    $data = [
        'users' => [
            [
                'name' => 'Taylor Otwell',
                'attributes' => [
                    'foo',
                ],
            ],
            [
                'name' => '',
                'attributes' => [
                    'foo',
                    'bar',
                    1,
                ],
            ],
        ]
    ];

    $validator = Validator::make($data, [
        'users.*.name' => 'required|string',
        'users.*.attributes.*' => 'string',
    ], [
        'users.*.name.required' => ':index :position',
        'users.*.attributes.*.string' => ':first-index :first-position :second-index :second-position',
    ]);

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 this pull request may close these issues.

4 participants