验证器对数组属性的翻译

问题描述 投票:0回答:2

当验证器属性为数组时,如何翻译键?

例如:

这是带有数组

'image.author'
'image.book'
的验证器。

$validator = Validator::make($request->all(), [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
    'image.author' => 'required',
    'image.book' => 'required',
]);

validation.php
上存在这些属性。

'attributes' => [
    'image.author' => 'image of author',
    'image.book' => 'image of book',
],

如果我使用tinker来调试:

__('validation.attributes.image.author');

“验证.属性.图像.作者”

输出正确应该是:

“作者形象”

laravel-8 laravel-validation laravel-localization
2个回答
0
投票

“author”和“book”应该是验证属性数组中“images”索引的索引。

'attributes' => [
    'image' => [
        'author' => 'image of author',
        'book' => 'image of book',
    ],
],

0
投票

我能够翻译这些的唯一方法是:

trans('validation.attributes')['image.author']; //'image of author'
trans('validation.attributes')['image.book']; //'image of book'

trans('validation.attributes')
返回包含所有键的数组。

© www.soinside.com 2019 - 2024. All rights reserved.