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

Uteev's avatar
Level 3

Empty input values in rules() method of FormRequest

Hello everyone!

I have a strange issue. I can't access input parameters in rules method of FormRequest. The following code returns empty array:

public function rules()
    {
        dd($this->all());
        $rules = [
            'name'  => 'required|min:5|max:255|unique:products,name,' . $this->get('id'),
            'sku'   => 'required|max:255|unique:products,sku,' . $this->get('id'),
            'slug'  => 'required|max:255|unique:products,slug,' . $this->get('id'),
            'price' => 'required|numeric',
        ];



        /*foreach ($this->input('variations') as $index => $variation) {
            $rules['variations.' . $index . 'sku'] = 'unique:variations,sku,' . $this->input('productVariationIDs')[$index];
        }

        dd($rules);*/

        return $rules;
    }

However if I try to access the same input inside the other method of the class then I get it without any issue. For example in the authorize method:

public function authorize()
    {dd($this->all());
        // only allow updates if the user is logged in
        return backpack_auth()->check();
    }

I also find out that rules() method is called before authorize(), so I assume that request is not fully instantiated. But this is very strange as after googling I couldn't find anybody with the same issue.

So my question is how can I access input parameters inside the rules() method of class that extends FormRequest class?

This is Laravel 5.7 with installed Backpack.

0 likes
5 replies
munazzil's avatar

in your function you have to pass param

public function rules(Request $request,Rules $rules)
Uteev's avatar
Level 3

Hi munazzil! Thank you for your reply. Could you give more information? Request and Rules are instances of what class?

I tried to inject App\Http\Requests\Request as Request $request but I was not successful:

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR)
Too few arguments to function App\Http\Requests\ProductUpdateRequest::rules(), 0 passed in /home/vagrant/code/playground/vendor/backpack/crud/src/PanelTraits/RequiredFields.php on line 25 and exactly 1 expected
1 like
Snapey's avatar

it is strange, and i dont see @munazzil suggestion being of any help since your form request is already extending request class.

How do you reference this class in your controller ?

MichaelTop's avatar

@Snapey Hello, I have the same issue. In controller I reference class like this: public function show(ShowUserRequest $request): JsonResponse { try { return response()->json([ 'success' => true, 'user' => User::query()->where('id', $request->validated('id'))->first() ]); } catch (\Exception $e) { return response()->json([ 'success' => false, 'message' => $e->getMessage(), ], 500); } }

Please or to participate in this conversation.