Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Artwork's avatar

Access custom public field/method of ValidationRule instance in FormRequest

Dear Developers, Community,

Thank you for the marvel...

As we may see, there is a public method videoData() that exposes a variable with valid video data gathered from remote.

How would you access the method videoData(), or the $videoData field, in the validated FormRequest that is then to be used in controller?

The main rationale is to somehow get the video data from the rule instead of doing it again elsewhere or caching in memory.

Best and kind regards

0 likes
5 replies
LaryAI's avatar
Level 58

To access the custom method (videoData()) or the video data from your custom ValidationRule instance after validation in a FormRequest, you need a way to retrieve the specific rule instance that was used during validation. By default, Laravel does not expose the rule instances after validation—they are instantiated and used internally.

However, you can work around this by injecting the rule instance into your FormRequest, storing it as a property, and then accessing it after validation.

Step-by-step Solution

1. Store the Rule Instance in the FormRequest

In your FormRequest, define a property to hold the rule instance:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\UserMusicPlayerRule;
use App\Models\User;

class StoreVideoRequest extends FormRequest
{
    /** @var UserMusicPlayerRule */
    public UserMusicPlayerRule $musicPlayerRule;

    public function rules()
    {
        // Instantiate the rule and store it on the request
        $this->musicPlayerRule = new UserMusicPlayerRule($this->user());

        return [
            'video_url' => [$this->musicPlayerRule],
        ];
    }
}

2. Access the Data in the Controller

After validation, you can access the videoData() method from the rule instance stored on the request:

public function store(StoreVideoRequest $request)
{
    $videoData = $request->musicPlayerRule->videoData();

    // Now you can use $videoData as needed
}

Notes

  • This approach works because the same rule instance is used for validation and then available for later access.
  • If you use validation directly on the controller (e.g., $request->validate()), this pattern won't work. It's only possible when you control the instantiation of the rule, such as in a custom FormRequest.

Summary:
Store the rule instance as a property on your FormRequest, then access its data after validation in your controller.

Let me know if you need a more advanced solution (e.g., for array rules or multiple rules)!

Artwork's avatar

To be frank, it's the first time I see a validation rule to be exposed to a request and not the safe/validated data. Is that adequate/common? Doesn't the rule exposed like that imply it can be executed/validated again somewhere in the request that is redundant?:

$request->musicPlayerRule->validate(/*...*/));
JussiMannisto's avatar

A validation rule class should only handle validation, not data mutation. If you want the constructed videoData to be part of the validated input, you should override the validated() method in the form request class, e.g.:

public function validated($key = null, $default = null): array {
	$data = [
		...parent::validated(),
		'videoData' => MusicPlayer::videoFromUrl($this->input('video_url'), $this->user()),
	];

	return $key === null 
		? $data 
		: data_get($data, $key, $default);
}

Yes, this calls the videoFromUrl() method twice, first during validation and then in the form request. But this provides a clear separation of concerns.

1 like
Artwork's avatar

@JussiMannisto the validation may only pass if the video is valid upon its content retrieval.

Yet, how would method validated obtain the already got video data within the validation rule that is based on the content itself? That static method, ::videoFromUrl downloads video metadata, and two requests to the remote inside both rules (or after) and validated is not an option. And, caching it would make sense inside validated or later, too. Hence, the question.

Meanwhile, thank you very much regarding the mutation note, indeed!

JussiMannisto's avatar

@Artwork I missed that the video data is retrieved from a remote location. A rule class definitely shouldn't be making any network calls. You should rethink the approach as it isn't actual input validation.

I'd get rid of the UserMusicPlayerRule class. I'd validate the input in the form request, then download and check the video data in the controller, returning an appropriate error message if the data isn't valid or there's a connection issue.

1 like

Please or to participate in this conversation.