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'
]
]
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With