Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel translate values required_if

I am using Laravel version 5.2.45. Currently I have some troubles with translating required_if rule. When I am using the required_if,field,value it's printing out the fields value in the error validation message, which in this case is either 1 or 0. that is not very readable.

For example:

Field 1 is required if type is 0

Would like:

Field 1 is required if type is default

Is there any way to translate the values of the rquired_if value/:value?

Controller:

$customerVal = Validator::make($request->all(), [
        'field1' => 'required_if:type,0',
        'field2' => 'required_if:type,0',
    ]);

View:

@if (count($errors) > 0)
        <div class="modalMsg alert">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

I can see that Laravel has this in the language section:

'required_if'          => ':attribute is required when :other are  :value.',

So it's basically :value I need to translate(dynamically). I have tried below, but that does not replace 0:

'attributes' => [
'field1' => [
            '0' => 'test'
        ]
]
like image 521
simon Avatar asked Feb 09 '17 14:02

simon


People also ask

How do I translate validation errors in Laravel?

Laravel's built-in validation rules each has an error message that is located in your application's lang/en/validation.php file. Within this file, you will find a translation entry for each validation rule. You are free to change or modify these messages based on the needs of your application.

Where does Laravel look for translations?

Laravel looks for translations in the resources/lang/<languagecode>/<filename>.php file. E.g. the english translation for welcome.menu.documentation is retrieved from the file resources/lang/en/welcome.php.

How to add a new language to a Laravel project?

After opening the project you should see a screen with your translations: Adding a new language is easy: Back on the main screen you should now see text fields for the new language. Click Save Project. BabelEdit asks you for the file name of its project file. Name it laravel-demo.babel and store it in your project folder.

How to add multiple dots to a translation in Laravel?

Using multiple dots allows you to structure the translations within a file. Create the resources/lang/en/welcome.php file and paste the following content in it: Refresh the app to see the english translations. The loader for Laravel php files is quite restrictive. It only allows one single return statement with an array of translations.


1 Answers

You are trying to translate values rather than attributes.

Open app/lang/en/validation.php file and add new array element:

'values' => [
    'type' => [
        '0' => 'default',
     ],
 ],

Found in laravel's github.

like image 50
shukshin.ivan Avatar answered Sep 22 '22 05:09

shukshin.ivan