-
-
Notifications
You must be signed in to change notification settings - Fork 259
Description
✏️ 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