Skip to content

FromRouteParameter ignored for validation rules for nested data objects #1112

@ragulka

Description

@ragulka

✏️ Describe the bug
When a nested data object uses the #[FromRouteParameter] attribute for a property, and tries to access this property in the ValidationContext payload, it's empty (not injected by the pipeline).

The root level data object does have the route parameter injected.

It looks like the InjectPropertyValuesPipe class does not work recursively on nested data objects. It only checks if the root data class properties have an InjectsPropertyValue attribute.

↪️ To Reproduce
Provide us a pest test like this one which shows the problem:

class PostDetailsData extends Data
{
    public function __construct(
        #[FromRouteParameter('blog')]
        public Blog $blog,
        public string $slug,
    ) {
    }

    public static function rules(ValidationContext $validationContext)
    {
        $payload = $validationContext->payload;

        /** @var ?Blog $blog */
        $blog = $validationContext->payload['blog']; // blog will never be set here

        return [
            'slug' => [Rule::unique('post_details')
                ->ignore($payload['id'])
                ->where('blog_id', $blog->id)
            ],
        ];
    }
}

class PostData extends Data
{
    public function __construct(
        #[FromRouteParameter('blog')]
        public Blog $blog,
        public PostDetailsData $details,
    ) {
    }
}

Note: this just provides the minimal case for reproduction, which should result in ErrorException: Undefined array key "blog" being thrown.

✅ Expected behavior
I expect the route parameters to be injected to all nested Data objects before validation.

🖥️ Versions

Laravel: 12
Laravel Data: 4.18
PHP: 8.4

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