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

Tamas_hi's avatar

Laravel validation error message format - error key

The default ValidationException looks like this (source: Laravel Official documentation)

{
    "message": "The team name must be a string. (and 4 more errors)",
    "errors": {
        "team_name": [
            "The team name must be a string.",
            "The team name must be at least 1 characters."
        ],
        "authorization.role": [
            "The selected authorization.role is invalid."
        ],
        "users.0.email": [
            "The users.0.email field is required."
        ],
        "users.2.email": [
            "The users.2.email must be a valid email address."
        ]
    }
}

It would seem obvious for me to have this built-in natively, but I can't find a way to return the exact rule that was broken. For example:

$request->validate([
'name' => 'required'
]);

Then the returned error object should contain name.required, or something to know on the frontend which rule was broken. Now you can only get a localized message, and the 'name' key, and you have no idea which rule was broken. Doesn't Laravel have a way to return the broken rule, like name.required, name.unique, etc...

Thanks.

0 likes
6 replies
gych's avatar

In resources/lang/en.php you can add these messages for validation

return [
    'required' => 'The :attribute field is required.',
 	'unique' => 'The :attribute is not unique and already in use.',
    // add more validation message keys and strings as needed
];

For name required you will receive this message

The name field is required.

For name unique you will receive this message

The name is not unique and already in use.

Is this what you're looking for?

Tamas_hi's avatar

Thanks for the reply. These are the actual validation messages that are present in the ValidationException, I already have these.

But let's say I dont store/check the user's locale on the backend, but locale changing is possible on a vue frontend.

So I would need something (a key) that I can use to create localized error messages on the frontend. And that key could very well be the rule, that was broken (for example: date => before_or_equal). I would like to find a way to return not just a localized message, and a name (like in my original post), but a key that shows which rule was broken. This "before_or_equal" is something you cannot figure out with the current setup, you will just get a localized message that is so-so grammatically correct in other languages and you can't create your own error messages on the frontend.

gych's avatar

@Tamas_hi Are you using Laravel as API with separate Vue SPA for the frontend ?

gych's avatar
gych
Best Answer
Level 29

@Tamas_hi Ok here are two options

** OPTION 1 **

Since you're not going to use the validation messages from the laravel lang file you could change them to this:

return [
    'required' => 'required',
 	'unique' => 'unique',
];

If than for example team_name doesn't pass unique and required validation it will return this error.

"team_name": [
            "required",
            "unique"
        ],

** OPTION 2 ** Create a custom validationexception that extends ValidationException

Doing this will require some more work.

This example will head you in the right direction but you'll still have to make some changes to achieve what you're looking for https://laraveldaily.com/post/laravel-validation-completely-customize-error-message-format

1 like
Tamas_hi's avatar

@gych Yes, option 2 is something I have tried, but it seems that it is not possible to get the actual broken rule from the ValidationException implementation.

Regarding option 1... Hmm, that sounds like a good idea! I didn't think about it this way, that can actually work. Thanks for the suggestion, I'll go with this one.

1 like

Please or to participate in this conversation.