2

I know laravel provides customisable error messages for each validation rules but is there a way to change the message part of the json result? I can't seem to find anything on the docs.

{
    "message": "The given data was invalid.",
    "errors": {
        "email": [
            "The email has already been taken."
        ]
    }
}
2
  • 1
    how do you validate it? Commented Dec 30, 2019 at 13:47
  • just using the FormRequest an placing the validation rules inside Commented Dec 30, 2019 at 14:07

3 Answers 3

3

You can definitely create your own Exception class and instruct laravel to use it to reply the response.

Assuming your own custom Exception class is Illuminate\Http\Exceptions\HttpResponseException you only need to override failedValidation method in your form request class and have something like this.

use Illuminate\Http\Response;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

...

/**
 * @param Validator $validator
 * @throws HttpResponseException
 */
protected function failedValidation(Validator $validator)
{
    throw new HttpResponseException(
        response()->json(
            [
                'message' => 'My message',
                'errors' => $validator->errors()->get('*') //or ->all() instead of get()
            ],
            Response::HTTP_UNPROCESSABLE_ENTITY
        )
    );
}

The above would ensure your custom error response format is used. You can also add it to a Trait and include it in all your request class or just create a base Request class that others should extend.

Sign up to request clarification or add additional context in comments.

3 Comments

I know this is a couple of years old, but... could you possibly show how to instruct Laravel to use the custom exception instead of its built-in one?
By design, declaring this in your form request will overwrite failedValidation method inherited by the form request which means a failed response would come out based on your definition. If you mean to instruct Laravel to throw your custom exception instead of all Laravel built in, then maybe you want to look into Handler.php and intercept in-built exception before they reach your users. Generally look for topics such as error handling in Laravel.
Ah! I missed in your form request class in the answer. Thanks!
3

Add custom message on your venue class as follow:

public static function messages($id = '') {
return [
    'name.required' => 'You must enter your name',
    'logo.required' => 'You must upload logo',
    'key.rules' => 'your messages'
];

And on your controller add messages as third parameter.

$this->validate($request, Venue::rules(), Venue::messages());

Comments

1

The “The given data was invalid.” message is actually the exception class message, which isn’t easily configurable:

https://github.com/laravel/framework/blob/e04a7ffba8b80b934506783a7d0a161dd52eb2ef/src/Illuminate/Validation/ValidationException.php#L46-L56

1 Comment

That's too bad. Is there a way to create my own exception and get laravel to throw the custom exception instead of ValidationException? That way i can just copy the existing features and add in my own custom ones.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.