Posted

in

,

by

Laravel Precognition on a resource route

In this post I will provide some simple steps to implement Laravel Precognition form validation on a resource route as it might be difficult to understand reading the official documentation.

Let’s start with the web.php routes file. You should have defined a resource route like this:

...

Route::resource('items', ItemController::class)

...

And you’re probably doing the validation in the ItemController.php something like this:

...

public function store(Request $request): RedirectResponse
    {
        Validator::make($request->all(), [
            'name' => 'required|min:5',
            'description' => 'nullable|string',
            'is_attribute' => 'boolean',
            'is_location' => 'boolean',
            'journal_no' => 'nullable|integer|unique:items,journal_no',
        ])->validate();

...

}

...

It might not be the exact way you do the validation but nevertheless I provide it as an example.

My frontend in running on Inertia + Vue and depending on your frontend you might need to install a different package. You can find the documentation here. Read the installation and frontend implementation carefully as I will only cover the backend implementation.

So, once you finish converting your frontend components to use Precognition it is in fact as simple as attaching the middleware to your resource route like this:

use Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests;

...

Route::resource('items', ItemController::class)->middleware([HandlePrecognitiveRequests::class]);

And then you have to create a form request as per documentation ‘php artisan make:request StoreItemRequest’ and then move the validation into it:

...
/**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true; #I DO MY AUTHORISANTION ON CONTROLLER SO JUST SET IT TO 'TRUE'
    }
/**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            'name' => 'required|min:5',
            'description' => 'nullable|string',
            'is_attribute' => 'boolean',
            'is_location' => 'boolean',
            'journal_no' => 'nullable|integer|unique:items,journal_no',
        ];
    }
...

Then update your controller like this:

use App\Http\Requests\StoreItemRequest;

...

public function store(StoreItemRequest $request): RedirectResponse
    {
$validated = $request->validated();

...

}

...

Notice the removal of the validation. Don’t forget to import the form request if your IDE doesn’t do that for you.

And that’s it! Now you should have a working Precognition form validation assuming you implemented it on the frontend correctly!

Note: do not get confused if you start seeing 422 error in the console, it means the validation is working!

Comments

Leave a Reply