Skip to content

ValidationRuleParser does not handles the explode for multiple chained non-nested Date or Numeric Rule objects #54717

@AlexandreMeledandri

Description

@AlexandreMeledandri

Laravel Version

11.x

PHP Version

8.4.4

Database Driver & Version

No response

Description

The current code in ValidationRuleParser explodeExplicitRule function does not handles the explode for multiple chained non-nested Date or Numeric Rule objects. It falls into the condtion that checks if the rule is an object and wraps the rule in an array as a string cast without exploding it at the pipe symbol.

protected function explodeExplicitRule($rule, $attribute)
    {
        if (is_string($rule)) {
            return explode('|', $rule);
        }

        if (is_object($rule)) {
            // The bug lies here, prepareRule falls to a return of (string) $rule
            return Arr::wrap($this->prepareRule($rule, $attribute));
        }

        $rules = [];

        foreach ($rule as $value) {
            if ($value instanceof Date || $value instanceof Numeric) {
                $rules = array_merge($rules, explode('|', (string) $value));
            } else {
                $rules[] = $this->prepareRule($value, $attribute);
            }
        }

        return $rules;
    }

Steps To Reproduce

A simple validation like the following is failing

$rules = [
    'date' => Rule::date()->format('Y-m-d'),
];

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