Gabriel Caruso
Gabriel Caruso

Reputation: 869

Customize Laravel FormRequest autorize method validation

Laravel allows us to authorize, or not, a FormRequest to be processed via the authorize method. If the request isn't authorized, it will throw a \Illuminate\Auth\Access\AuthorizationException exception, with a message:

This action is unauthorized.

Is there somehow to customize this message?

See that I want to customize the message itself. Customizing the error messages of attributes I know it is possible!

Upvotes: 8

Views: 4174

Answers (2)

Rwd
Rwd

Reputation: 35200

To change the message you can add the following to your FormRequest class.

protected function failedAuthorization()
{
    throw new AuthorizationException('Your new message goes here.');
}

Don't forget to add the following to the class as well:

use Illuminate\Auth\Access\AuthorizationException;

Upvotes: 21

Ganesh
Ganesh

Reputation: 25

if you are trying to customise the message authourization exceptional message then use throw new exception in authorization controller itself in else part

Upvotes: 0

Related Questions