Squiggs.
Squiggs.

Reputation: 4474

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
}

Upvotes: 1

Views: 360

Answers (2)

Marco Feregrino
Marco Feregrino

Reputation: 663

. Use the messages() function to overwrite them

Function messages in your request file

    'email.required' => 'Er, you forgot your email address!',
    'email.unique' => 'Email already taken m8',

Upvotes: 0

Odin Thunder
Odin Thunder

Reputation: 3547

Write this in your Request class:

public function rules()
{
    return [ 
        'name' => 'unique'
    ]
}

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

}

Upvotes: 1

Related Questions