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

LaraBABA's avatar

How to test a rule?

Hi all,

I have a requestFile set this way:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProfileUpdateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'first_name' => ['required', 'max:255', 'string'],
            'last_name' => ['required', 'max:255', 'string'],
            'email' => [
                'required',
                Rule::unique('users', 'email')->ignore($this->profile),
                'email',
            ],
            'password' => ['nullable'],
            'business_name' => ['nullable', 'max:255', 'string'],
            'postcode' => ['nullable', 'max:255', 'string'],
            'business_phone' => ['nullable', 'max:255', 'string'],
            'vat_number' => ['nullable', 'max:255', 'string'],
            'active' => ['required', 'max:255', 'string'],
            'avatar' => ['nullable', 'image', 'max:1024'],
            'roles' => 'array',

        ];
    }
}

The request never reaches my controller so I assume that there is an error in the request itself. Is there a way to output errors in this case please from this file as I am not getting any output after the request.

Thanks

0 likes
9 replies
Sinnbeck's avatar

Do you mean an actual test? Like phpunit. Or just testing it in the browser

1 like
LaraBABA's avatar

@Sinnbeck Sorry, yes true, this can be understood differently.

What I meant is that the data from my form is sent and received via $this->profile . The data from $this->profile seems correct, but I am unable to find out why the "return" is not passing the validation.

In my controller, I never land to the "update method", the request seems to be invalided:

   public function update(ProfileUpdateRequest $request, Profile $profile)
    {
//logger('testing') <----it is never reaching this point.
}
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Or use dd inside the form request to inspect your variables

1 like
Swaz's avatar

You can see the validation errors by dumping them like this:

public function test_create_product()
{
    $response = $this->post('/products', [
	    'name' => 'something',
        //...
	]);

    // See what is failing
    dd(session('errors'));
}
Swaz's avatar

@martinbean I think @user476820 just wants to temporarily see why a test is not hitting the controller. It's getting stuck in validation but doesn't know what provided data is invalid.

1 like
martinbean's avatar

@Swaz …and the above will show validation errors if validation unexpectedly fails.

LaraBABA's avatar

Ok everyone I worked it out at the end.

I am using livewire(inherited an app that uses this, and I am not familiar with it). The rule was correct, but the x-input has a missing variable in blade which was set as "required" in the database. It looks like this type of app does not behave like a normal Laravel app as no errors were being output at all in the log. I was expecting something like "Missing data for table column XXXX".

I am still trying to figure out why no errors were being output....

Please or to participate in this conversation.