Laravel转换值required_if [英] Laravel translate values required_if
问题描述
我正在使用Laravel 5.2.45版本.目前,我在翻译required_if规则时遇到了一些麻烦.当我使用required_if,field,value时,它会在错误验证消息中打印出fields值,在这种情况下为1或0.这不是很可读.
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.
例如:
如果类型为0,则必填字段1
Field 1 is required if type is 0
想要:
如果类型为默认值,则必填字段1
Field 1 is required if type is default
有什么方法可以转换rquired_if value/:value的值?
Is there any way to translate the values of the rquired_if value/:value?
控制器:
$customerVal = Validator::make($request->all(), [
'field1' => 'required_if:type,0',
'field2' => 'required_if:type,0',
]);
查看:
@if (count($errors) > 0)
<div class="modalMsg alert">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
我可以在语言部分看到Laravel对此的提示:
I can see that Laravel has this in the language section:
'required_if' => ':attribute is required when :other are :value.',
所以基本上是:value我需要动态翻译.我在下面尝试过,但是不能代替0:
So it's basically :value I need to translate(dynamically). I have tried below, but that does not replace 0:
'attributes' => [
'field1' => [
'0' => 'test'
]
]
推荐答案
您正在尝试转换值而不是属性.
You are trying to translate values rather than attributes.
打开 app/lang/en/validation.php
文件并添加新的数组元素:
Open app/lang/en/validation.php
file and add new array element:
'values' => [
'type' => [
'0' => 'default',
],
],
在 laravel的github 中找到.
这篇关于Laravel转换值required_if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!