Search code examples
phplaravellaravel-5.3

Laravel Extend Rule Message


I am using a 'unique' validation Rule within Laravel, which is currently within a FormRequest. I'm trying to customise the message returned from this Rule and I can't see in the documentation where Laravel generates this message.

I realise it is possible to fully extend the Validator and create my own custom one, but all I need to do is customise the 'Unique' rule message. The class in the documentation is here:

https://github.com/laravel/framework/blob/5.3/src/Illuminate/Validation/Rules/Unique.php

any pointers on where internally this message is generated? For reference this is the current output:

{
  "message": "422 Unprocessable Entity",
  "errors": {
    "user_id": [
      "The user id has already been taken."
    ]
  },
  "status_code": 422
}

Solution

  • Write this in your Request class:

    public function rules()
    {
        return [ 
            'name' => 'unique'
        ]
    }
    
    public function messages()
    { 
         return[
             'name.unique' => 'Write your own message ... '
         ]
    
    }