Skip to content

Not restoring parent Each's key on some conditions #823

Description

@jettero777

Description

In EachHandler there are 2 returns without restoring in context parent Each's key.

Code in question:

        $originalEachKey = $context->getParameter(Each::PARAMETER_EACH_KEY); // <== storing parent key

        foreach ($value as $index => $item) {
            if (!is_int($index) && !is_string($index)) {
                return (new Result())->addError($rule->getIncorrectInputKeyMessage(), [ // <== bug, return without restoring parent key
                    'property' => $context->getTranslatedProperty(),
                    'Property' => $context->getCapitalizedTranslatedProperty(),
                    'type' => get_debug_type($index),
                ]);
            }

            $context->setParameter(Each::PARAMETER_EACH_KEY, $index);

            $itemResult = $context->validate($item, $rules);
            if ($itemResult->isValid()) {
                continue;
            }

            foreach ($itemResult->getErrors() as $error) {
                $result->addErrorWithoutPostProcessing(
                    $error->getMessage(),
                    $error->getParameters(),
                    $error->getValuePath() === [] ? [$index] : [$index, ...$error->getValuePath()],
                );
            }

            if ($rule->stopOnError) {
                return $result; // <== bug, return without restoring parent key
            }
        }

        $context->setParameter(Each::PARAMETER_EACH_KEY, $originalEachKey); // <== restoring parent key

        return $result;

Package version

No response

PHP version

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions