Laravel translate values required_if
Asked Answered
D

1

7

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'
        ]
]
Dismast answered 9/2, 2017 at 14:20 Comment(0)
T
11

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.

Tisman answered 9/2, 2017 at 22:53 Comment(1)
Thanks! Also now documented at laravel.com/docs/validation#specifying-values-in-language-filesStringent

© 2022 - 2024 — McMap. All rights reserved.