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);