phplaravellaravel-8responsecustomvalidator

Laravel custom validator returns page instead of json


I made a controller to store tasks, it looks like:

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

        return response()->json($request);
    }

For the request validation i made a custom validator, that looks like

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreTaskRequest 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 [
            'project_id' => 'required',
            'project_name' => 'required',
            'task_id' => 'required',
            'task_name' => 'required',
            'startDate' => 'required',
            'endDate' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'project_id.required' => 'required!',
            'project_name.required' => 'required!',
            'task_id.required' => 'required!',
            'task_name.required' => 'required!',
            'startDate.required' => 'required!',
            'endDate.required' => 'required!',
        ];
    }
}

When i make a post request to that controller with incorrect data it returns an html page but when i post it with the correct data it returns the request as json

By the way i make my post request with: reqbin.com

Post request headers: 
X-Csrf-Token: ....
Content-Type: application/json

Post Params:
{
  "project_name": "Wiza",
  "task_id": 1,
  "task_name": "test 1",
  "startDate": {
    "week": 1,
    "start" : 11,
    "year": 2022
  },
  "endDate": {
     "week": 1,
    "start" : 11,
    "year": 2022
  }
}

Does anyone have any idea why its returning a html page instead of an validation error?

Edit:

api.php is empty web.php

Route::post('api/v1/tasks/', [TaskController::class, 'store']);

//Also tried
Route::resource('api/v1/tasks/', TaskController::class);

Solution

  • Add this code in your custom validator.

    /**
     * Get the error messages for the defined validation rules.*
     * @return array
     */
    protected function failedValidation(ValidationValidator $validator)
    {
        throw new HttpResponseException(response()->json([
            'message' => implode('', collect($validator->errors())->first()),
            'status' => true
        ], 422));
    }