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

joynal's avatar

Request class not returning validation errors

Request class not returning validation error on AJAX Requests. [laravel 5.2] This is my validation class

<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class MovieRequest extends Request
{
  public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title' => 'required|max:255',
            'actors' => 'required',
            'plot' => 'required',
            'year' => 'required',
        ];
    }
}

and this is my controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\MovieRequest;
use App\Models\Movie;

class MovieController extends Controller
{
    public function store(MovieRequest $request)
    {
        dd($request->all());
    }
}

Instead of showing errors it returns 404 error.

My route definition is fine, it's working when I request with valid data. This is the route.

$router->resource('movies', 'MovieController');
0 likes
14 replies
d3xt3r's avatar

double check your routes definition ...

Jayanthkumar's avatar

add the route in web.php (web.php is default route while creating project) or add the route in ur custom route.php file

Snapey's avatar

I think there can be an issue with ->back() if there is something odd with the names of the routes.

So, when you fail validation, its going to try and reload the form again. Open your browser network tools then post the form. If there is a validation error you will see a 302 redirect response. The 302 is a GET request to a specific URL. This URL must be resolvable by the routes.

joynal's avatar

@Snapey I'm not using ->back(), testing from postman. When rules are fails it should return JSON response with a 422 HTTP status code.

Snapey's avatar

How does the framework know its an ajax request? Are you specifying a header to ask for a json response?

joynal's avatar

@Snapey Yes! man. Body -> raw JSON and header -> 'application/json'. I also tested same things laravel 5.3 same problem :(

Snapey's avatar

I think we need to know more about the routes file and the URL you are calling?

And you are saying that if you have

    public function store(MovieRequest $request)

then you get 404, but if the function is like this;

    public function store(Request $request)

then it works fine? No other changes?

joynal's avatar

@Snapy I'm talking about custom request class, MovieRequest is my custom request class.

Snapey's avatar

I know, and I am saying does it reach your controller if you don't use your custom request class?

joynal's avatar

Yes. It's also reached with custom request class when rules get passed .

Snapey's avatar
Snapey
Best Answer
Level 122

I just tested with postman (on 5.3) and it seems to work exactly as expected.

Controller;

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\TestRequest;


class TestController extends Controller
{
    public function store(TestRequest $request)
    {
        return ([
            $request->Hello,
            $request->Numerics
            ]);
    }
}

TestRequest

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class TestRequest 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
     */
    public function rules()
    {
        return [
            'Hello'=>'Required',
            'Numerics' => 'Required'
        ];
    }
}

web.php

Route::post('/test','TestController@store');

The only other thing I did to ease testing was to add '/test' to the csrf middleware exceptions

Then used postman. Headers;

Content-Type:application/json
Accept:application/json

Body set to Raw data

{"Hello":"World"}

422 response;

{"Numerics":["The numerics field is required."]}
4 likes
joynal's avatar

Thank you so much @Snapey . 'Accept:application/json' this header I was missing.

1 like
Hash's avatar

@joynal was struggling to know why the form request were not working on APIs, thanks to your comment managed to solved it by adding the above header XD

Please or to participate in this conversation.