Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kay899's avatar

Extend validation.php Custom Validation Attributes

Hi,

at the bottom of the validation.php lang file there is an "attributes" array.

How can I load my attributes" from my lang files into that area?

I use an individual lang file per model which contains all my translationed fields.

Thanks Andreas

0 likes
4 replies
kay899's avatar

Does someone has an idea on that?

rodrigo.pedra's avatar

You can just override the attributes method on your form request ans use the trans(...) helper function, like so:

<?php namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class MyRequest extends FormRequest
{
    public function authorize()
    {
        return TRUE;
    }

    public function rules()
    {
        return [ 
            'field' => 'required',
            'other_field' => 'required' 
        ];
    }

    public function attributes()
    {
        // the attributes method replaces the :attribute placeholder on the validation messages
        // with given attribute names
        
        // You can use the trans(...) helper function here to get your 'localized' from 
        // resources/lang/{language}/{file}
        
        // set your default and fallback locale in the config/app.php file
        // I will assume you are using English ('en')
        
        return [ 
            'field' => 'fancy field name' ,
            // the next one will look in resources/lang/en/attributes.php 
            // for a other_field key
            'other_field' => trans('attribtues.other_field') 
        ];
    }
}

note that the attributes method was added very recently in Laravel's Illuminate\Foundation\Http\FormRequest class, if I am not mistaken it was added in version 5.0.18

1 like
lprice's avatar

@rodrigo.pedra man I've tried the above method and it just doesn't seem to work... any idea if there is an issue or not... Here my code...


class RegistrationRequest extends FormRequest
{

    /**
     * Determine if the user is authorized to make this request.
     * @return bool
     */
    public function authorize()
    {
        return true;
    }


    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        return [ 'name'                  => 'required|max:255',
                 'password'              => 'required|confirmed|min:6|max:60',
                 'password_confirmation' => 'required|min:6|max:60', ];
    }


    public function attributes()
    {
        return [
            'password_confirmation' => trans( 'password.confirm' )
        ];
    }
rodrigo.pedra's avatar

Do you have a resources/lang/en/password.php file with a confirm key? Note that you are using password in singular, Laravel ships with a passwords.php file, but the trans(...) will look to the file and key combination under that path.

(Change the en folder for the language configured in the config\app.php file under the locale key.)

Like this:

// resources/lang/en/password.php

<?php

return [
    "confirm" => "Password confirmation should be equal to password field."
];

Please or to participate in this conversation.