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

Benounnas Oussama's avatar

Add property to the validation response

        $validator = Validator::make($request->all(), [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]); 
 
        // Retrieve the validated input...
        $validated = $validator->validated(); 

the exception will be thrown by default like:

{
    "message": ".......",
    "errors": [
        [
            "There was an error on....."
        ]
    ]
}

my question is how to add other properties in the error validation response like:

{
    "message": ".......",
    "prop1": ".......",
    "prop2": ".......",
    "errors": [
        [
            "There was an error on....."
        ]
    ]
}
0 likes
7 replies
vincent15000's avatar

@benounnas oussama Do you mean something like this using a form request ?

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class RecipeRequest 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 [
            'category_id' => 'required',
            'name' => 'required|max:255',
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {
        return [

            'category_id.required' => 'Le choix d\'une catégorie est obligatoire.',

            'name.required' => 'Le nom est obligatoire.',
            'name.max' => 'Le nom doit avoir un maximum de 255 caractères.',

        ];
    }
}
1 like
rodrigo.pedra's avatar
Level 56

The ValidationException gets serialized on the base Exception handler:

https://github.com/laravel/framework/blob/ffc35c59f1b3b8cc0b2316460b3b14780fbbbd51/src/Illuminate/Foundation/Exceptions/Handler.php#L500-L506

So you either override the invalidJson method on your app's local Exception handler and customize how it is serialized, or you catch the exception and serialize it manually in the spot you need it, for example:

$validator = Validator::make($request->all(), [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
]);

try {
    // Retrieve the validated input...
    $validated = $validator->validated();
} catch (ValidationException $exception) {
    return response()->json([
        'message' => $exception->getMessage(),
        'errors' => $exception->errors(),
        'prop1' => 'foo',
        'prop2' => 'bar',
    ], $exception->status);
}
3 likes
jlrdw's avatar

Also you can send what you want in the json response:

        if ($validator->fails()) {
            return response()->json($validator->errors(), 422);
        }

        // or

        if ($validator->fails()) {
            return response()->json(send a custom array here);
        }

2 likes

Please or to participate in this conversation.