Set friendly names for validation attributes in Laravel 4

I just lost half an hour on this, so I figured I’d help you save some precious time.

Every search on Google returned info on how to specify custom messages, which is nicely documented, but there’s currently nothing about friendly names on attributes on the docs.

After searching on Google, roaming through Laravel’s code and trying almost everything, I got to this comment on GitHub.

You may call setAttributeNames on a Validator instance now.

So, for example’s sake:

$rules = array(
    ‘username’ => ‘required|min:8|unique:users|integer’,
    ‘firstname’ => ‘required’,
    ‘lastname’ => ‘required’,
    ‘email’ => ‘required|email’,
    ‘password’ => ‘required|confirmed’

$messages = array(
    ‘required’ => ‘El campo :attribute es obligatorio.’,
    ‘email.required’ => ‘El campo de :attribute es requerido y debe ser una dirección válida.’,
    ‘validation.confirmed’ => ‘Las contraseñas no coinciden.’

$friendly_names = array(
    ‘firstname’ => ‘Nombre’,
    ‘lastname’ => ‘Apellido’

$validator = Validator::make(Input::all(), $rules, $messages);


Ignacio 13 January 2015 Reply

You’re welcome, I’m glad it was helpful for somebody ^^

morsaPT 22 January 2015 Reply

Thank you.
I’m using it like this:


and set inside my_model class the attribute names for better understanding.

Ignacio 14 February 2015 Reply

Yeah, that’s a good approach, thanks :)

Leave a Reply