Skip to content

[6.x] Don't return nested data from validator when failing an exclude rule#33435

Merged
taylorotwell merged 1 commit intolaravel:6.xfrom
hackel:fix-nested-exclude-validation
Jul 9, 2020
Merged

[6.x] Don't return nested data from validator when failing an exclude rule#33435
taylorotwell merged 1 commit intolaravel:6.xfrom
hackel:fix-nested-exclude-validation

Conversation

@hackel
Copy link
Copy Markdown
Contributor

@hackel hackel commented Jul 5, 2020

When using the exclude_if or exclude_unless validation rules, nested fields are excluded from being validated, however they are not being removed from the validated data.

This is a breaking change, however I would consider it a bugfix since the documentation is clear that "The field under validation will be excluded from the request data returned by the validate and validated methods."

The existing test case for this feature did not reflect the intended behaviour, so I just corrected that test rather than including a new one.

@GrahamCampbell GrahamCampbell changed the title Don't return nested data from validator when failing an exclude rule [6.x] Don't return nested data from validator when failing an exclude rule Jul 5, 2020
@taylorotwell
Copy link
Copy Markdown
Member

Please give an example.

@hackel
Copy link
Copy Markdown
Contributor Author

hackel commented Jul 8, 2020

The test case I modified in ValidationValidatorTest::testExcludeIf demonstrates the issue. It uses the following rules:

[
    'vehicles.*.type' => 'required|in:car,boat',
    'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|array',
    'vehicles.*.wheels.*.color' => 'required|in:red,blue',
    // In this bizzaro world example you can choose a custom shape for your wheels if they are red
    'vehicles.*.wheels.*.shape' => 'exclude_unless:vehicles.*.wheels.*.color,red|required|in:square,round',
]

With this data:

[
    'vehicles' => [
        [
            'type' => 'car',
            'wheels' => [
                ['color' => 'red', 'shape' => 'square'],
                ['color' => 'blue', 'shape' => 'hexagon'],
                ['color' => 'red', 'shape' => 'round', 'junk' => 'no rule, still present'],
                ['color' => 'blue', 'shape' => 'triangle'],
            ],
        ],
        ['type' => 'boat'],
    ],
]

This produces identical validated data, however we expect the exclude_if rule to remove the vehicles.*.wheels.*.shape values from the validated output when color != red (hexagon and triangle), but currently they remain because only the first level of of validated data is removed by the unset statements. Changing these to Arr::forget fixes this to also remove the nested data.

@taylorotwell taylorotwell merged commit 3a1c4ef into laravel:6.x Jul 9, 2020
@hackel hackel deleted the fix-nested-exclude-validation branch July 9, 2020 14:43
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.

2 participants