Laravel 10.45 - 枚举验证 - 无法设置自定义消息

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

我无法为枚举规则添加自定义验证消息。

我的规则定义为:

'components.*.component_type' => ['required', Rule::enum(BlueprintComponentType::class)]

自定义消息定义为:

'custom' => [ 'components.*.component_type' => [ 'enum' => 'The component type field should be ...', ], ],

输出为“The selected Components.0.component_type is invalid”,这是validation.enum的值

laravel validation enums
1个回答
0
投票

尝试使用

in
规则,因为
enum
本质上是
in
规则的简写。

'custom' => [
    'components.*.component_type' => [
        'in' => 'The component type field should be ...',
    ],
],

如果上面的代码不起作用,请尝试在 FormRequest 中定义它

public function messages()
{
  return [
    "components.*.component_type.Illuminate\Validation\Rules\Enum" => "The component type field should be ..."
  ];
}

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