Skip to content

Inconsistent validation behavior with empty arrays and nested rules #55657

Closed
@comhon-project

Description

@comhon-project

Laravel Version

11.44.2

PHP Version

8.3

Database Driver & Version

No response

Description

Hello everyone,

I've noticed an unexpected behavior when validating empty arrays with nested rules.

Here’s an example without any nested rules, which works as expected:

$validated = Validator::validate(['foo' => []], [
    'foo' => 'present|array',
]);
// $validated contains the "foo" key:  ["foo" => []]

However, when I add a nested rule:

$validated = Validator::validate(['foo' => []], [
    'foo' => 'present|array',
    'foo.bar' => 'integer'
]);
// $validated is empty: []

Expected:
the validated values should be the same and contain the "foo" key, since "foo.bar" is not required.

Steps To Reproduce

$validated = Validator::validate(['foo' => []], [
    'foo' => 'present|array',
]);
// $validated contains the "foo" key:  ["foo" => []]

$validated = Validator::validate(['foo' => []], [
    'foo' => 'present|array',
    'foo.bar' => 'integer'
]);
// $validated is empty: []

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions