Laravel's "Given data is invalid"when using a form request

Related searches

So I have a Form Request that has validation rules and some messages.

This is my function in the controller (called via vue.axios)

public function store(TaskFormRequest $request, TaskService $service): JsonResponse
    $data = $request->validated();

    if (!$task = $service->create($data)) {
        return JsonResponse::create(['message' => __('Can\'t create Task')], 400);

    return JsonResponse::create(
            'task_id' => $task->id,
            'message' => __('Task was successfully added.'),

It works OK, except that the moment store is called on (and $request is being considered TaskFormRequest instance) when the data is not validated it returns "The given data is invalid" JsonResponse in addition to the error messages from the form.

This would be fine, but the message itself is buried deep in Vendor and I can't apply any translations on it without it being a hassle.

This is my form request:

public function authorize(): bool
    return $this->user()->role === 'administrator';

public function rules(): array
    return [
        'subject' => 'required',
        'date' => 'nullable|date_format:Y-m-s',
        'user' => 'nullable|exists:users,id',
        'task_list_id' => 'required|exists:task_lists,id',
        'privacy' => 'array',

public function messages() {
    return [
        'subject.required' => __("Task name is empty")

I want to all together stop this response and just leave the errors from the form. Or at the least customize it.

EDIT: I think the problem is that the request is empty? If I enter a null default in the store function's arguments, I can check for empty $request and return a JsonResponse, but then it doesn't go through the normal validation process.

I was also struggling with the same problem. You would get "The given data is invalid" error if your validation code is in try block. Remove it out from try block and you will get proper error response as Laravel gives.

laravel/laravel: A PHP framework for web artisans, Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the� Laravel is a web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.

What happen when you remove you messages() function ?

(date_format:Y-m-s seems wrong, should'nt it be date_format:Y-m-d ? )

laravel/framework, Laravel is a powerful MVC PHP framework, designed for developers who need a simple and elegant toolkit to create full-featured web applications. Laravel was created by Taylor Otwell. This is a brief tutorial that explains the basics of Laravel framework.

The validator throws a Illuminate\Validation\ValidationException that you can intercept in the App\Exceptions\Handler->render() before it is sent to the parent Class in the vendor.

if ($exception instanceof ValidationException) {
    //choose the structure you want. the Validation exception has many methods
    return response()->json(['status' => 'error', 'message' => $exception->getMessage()], 422);

edit: to intercept the error in the form request itself, declare the following method:

public function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
    throw (new \Illuminate\Validation\ValidationException($validator))

Laravel, Laravel is a PHP framework that uses the MVC architecture.

Laravel Tutorial Web application development should be creative as well as enjoyable work so that it can realistically complete the user experience. Nowadays, users need more workability functionality in web pages.

PHP web frameworks are recognized as the best frameworks for web app development. In the same way, Laravel framework based on model-view-controller

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as: Simple, fast routing engine.

  • Then it returns the basic "Task name is required" or whatever in addition to the "The given data is invalid". Yeah, there was a typo in the date :) It's not required so I wasn't even filling in any dates.
  • are you sure the __("Task name is empty") si returning anything ? Does your language json file is correct ?
  • It does - the language system in this app is bizarre, but it works. Problem is only with "The given data is invalid" json message response.
  • but is there any way to intercept and cancel the whole response only for this particular formrequest
  • @MaximilianBerbechelov edited the answer. you can intercept the error by FormRequest if you need to. what's inside the method is the default result implemented by laravel