Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 403 forbidden on custom Request validation

Tags:

php

laravel

I've followed the docs here:

https://laravel.com/docs/5.8/validation#form-request-validation

I created a custom request StoreName

php artisan make:request StoreName

Then added the following validation rules:

public function rules()
{
    return [
        'name' => 'required|max:255|min:4'
    ];
}

Then as per the documentation type-hinted this in my controller:

public function store(StoreName $request)
{
    $validated = $request->validated();
}

However, when I send a post request to this endpoint I'm returned a 403 from Laravel. When I remove the StoreName custom validation and simply type-hint the standard Laravel Illuminate\Http\Request the request works fine (after removing the validated() method obviously).

So the 403 is coming from my custom validation request and I have no idea why? I've checked the file permissions on the StoreName.php file and they are the same as every other file in the project.

I'm using php artisan serve for my development server so no funky Apache/Nginx configs overwriting things either. All other endpoints work apart from this one when the custom validation request is applied.

What could the problem be?


EDIT:

It's probably worth noting I've not changed the default authorize() method Laravel generates in the new custom request validation either:

public function authorize()
{
    return false;
}
like image 509
twigg Avatar asked May 21 '19 09:05

twigg


People also ask

What is forbidden error in laravel?

Laravel - Forbidden You don't have permission to access / on this server.

How do I trigger a 403 error?

The most common cause of a 403 Forbidden Error is simply inputting an incorrect URL. As discussed before, many tightly secured web servers disallow access to improper URLs. This could be anything from accessing a file directory to accessing a private page meant for other users.


1 Answers

In your Custom request class you've got a method like this:

public function authorize()
{
    return true; // this is false by default which means unauthorized 403
}
like image 124
nakov Avatar answered Sep 20 '22 08:09

nakov