Localization of the EXISTS rule when using the @Rules instruction in Laravel 6, Lighthouse 5, and Module
1. When the language area is en , when the validated parameter does not exist, the response: The selected theme ID is invalid. as shown in Figure 1
{
"errors": [
{
"message": "Validation failed for the field [onlineStoreThemeEditorSessionCreate].",
"extensions": {
"validation": {
"themeId": [
"The selected theme id is invalid."
]
},
"category": "validation"
},
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"onlineStoreThemeEditorSessionCreate"
],
"trace": ...
}
],
"data": {
"onlineStoreThemeEditorSessionCreate": null
}
}
2. When the language area is zh_cn, when the validated parameter does not exist, the response: the selected Theme ID is invalid. Expected: The selected theme ID is invalid. as shown in Figure 2
{
"errors": [
{
"message": "Validation failed for the field [onlineStoreThemeEditorSessionCreate].",
"extensions": {
"validation": {
"themeId": [
"所选的theme id无效。"
]
},
"category": "validation"
},
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"onlineStoreThemeEditorSessionCreate"
],
"trace": ...
}
],
"data": {
"onlineStoreThemeEditorSessionCreate": null
}
}
3. Check the GraphQL code implementation, /modules/themestore/resources/graphql/theme_editor.graphql
extend type Mutation {
"创建主题编辑会话"
onlineStoreThemeEditorSessionCreate(themeId: ID! @rules(apply: ["exists:theme_asset,theme_id"])): ThemeEditorSessionCreatePayload @field(resolver: "Modules\\ThemeStore\\Resolver\\ThemeEditor\\CreateThemeEditorSessionResolver")
}
type ThemeEditorSessionCreatePayload
{
"会话"
session: Session!
}
4. Check the language file, /modules/themestore/resources/lang/en/validation.php, the custom attribute name of theme_id has been specified, but it does not take effect
[
'theme_id' => '主题ID',
],
];
5. Even if you put this configuration in the language file /resources/lang/en/validation.php, it still does not take effect, and the response: the selected Theme_id is invalid. However, its default is the configuration of this language file used. Put the validation rule message in a key named attributes in validation.php in any language, and the :attribute part of the validation message is not translated (module context).
'所选的:attribute无效。',
'attributes' => [
'theme_id' => '主题ID',
],
];
6. In Laravel v8.7.90, nwidart/laravel-modules 8.2.0, edit /resources/lang/en/validation.php , attributes will take effect.
/**
* 显示创建博客文章的表单
*
* @return Response
*/
public function create(Request $request)
{
$validator = Validator::make($request->all(), [
'theme_id' => 'required|exists:users,id',
]);
if ($validator->fails()) {
dd($validator->errors());
}
// 保存博客文章…
return view('blog::post.create');
}
'所选的:attribute无效。resources',
'attributes' => [
'theme_id' => '主题ID',
],
];
Illuminate\Support\MessageBag {#361 ▼
#messages: array:1 [▼
"theme_id" => array:1 [▼
0 => "所选的主题ID无效。resources"
]
]
#format: ":message"
}
7. Decide to simulate the implementation of step 6 in the current version: Laravel V6.20.43, Nwidart/Laravel-Modules 7.3.0. To determine whether it is related to the version of the framework and module package, which leads to the problem that Attributes does not take effect in the root directory of the application. Determined to be irrelevant. Then the reason is Lighthouse 5.
':attribute必须是一个数组。',
'attributes' => [
'filter' => '过滤器'
],
];
{
"message": "The given data was invalid.",
"errors": {
"filter": [
"过滤器必须是一个数组。"
]
},
"status_code": 422,
"debug": {
"line": 385,
"file": "E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Validation\\Validator.php",
"class": "Illuminate\\Validation\\ValidationException",
"trace": ...
}
}
8. Reference:https://github.com/nuwave/lighthouse/issues/1773, although the translation function has been implemented on the @rules instruction, but the implementation is not thorough enough. That is, the verification attribute is not supported. Developer suggestion:https://lighthouse-php.com/master/security/validation.html#validator-classes. Defining rules via any PHP code are supported.
9. It is not ready to write a custom rule for the time being. It is decided that in a non-English environment, the attribute name is not translated into the corresponding language, and it will be accepted for the time being. And the language file is in the root directory, not in the corresponding module directory.

