【问题标题】:Laravel display validation error in multiple languages at the same timeLaravel同时显示多种语言验证错误
【发布时间】:2022-01-18 17:13:25
【问题描述】:

我正在努力同时返回多种语言的验证错误。

我有一个控制器,它注入了一个扩展 FormRequest 的类,并且我正在覆盖“failedValidation”,并且在那里我收到了验证器错误消息。

    public function store(SysUserStoreRequest $request)
    {
      // ...
    }

    class SystemUserStoreRequest extends ApiRequest
    {
      // This extends another class ApiRequest
      Here I defined rules()
    }
    
    class APIRequest extends FormRequest
    {
      // Here I override the FailedValidation method.

      protected function failedValidation(Validator $validator)
      {
        throw new HttpResponseException($this->response($validator->getMessageBag()->toArray()));
      }
    }

以上代码当前以默认语言返回错误。 我可以通过更改中间件中的语言环境将响应更改为以不同的语言显示,但我正在处理需要返回一个新的验证错误结构的要求,其中每个字段在 en 和 fr 中都有错误。

我需要如下结构:

{
  "detail": {
      "email": {
        "en-CA" : [
          "The email has already been taken."
        ],
        "fr-CA" : [
          "The french text."
        ]
      },
      "first_name": {
        "en-CA" : [
          "The first name must be at least 5.",
          "The first name must be an integer."
        ],
        "fr-CA" : [
          "The french text",
          "The french text."
        ]
      }
  }
}

所以我厌倦了重写 failedValidation 方法并执行以下操作:

        $validation_structure = [];
        if ($validator->fails()) {
            $failedRules = $validator->failed();
        }

        

在获得所有失败的规则后,我可以从每个语言环境的 lang 文件夹中获取字符串,并获取字段和规则的字符串并使用生成消息

            $x[] = __('validation.unique', [], 'fr-CA');
            $x[] = __('validation.unique', [], 'en-CA');

这会给我两个labguages中的字符串,但我不知道如何替换:attributes、:values和其他各种字符串替换。

【问题讨论】:

    标签: php laravel validation laravel-8 laravel-validation


    【解决方案1】:

    您可以覆盖 SystemUserStoreRequest 将返回的消息包以格式化消息。

    class SystemUserStoreRequest extends ApiRequest
    {
        public function rules()
        {
            return [
                'email' => 'required|unique:users,email,' . $this->id,
            ];
        }
    
        public function messages()
        {
    
            return [
                'email.required' => [
                   'nl' => __('validation.required', ['attribute' => __('portal.email', [],'nl')], 'nl'),
                   'en' => __('validation.required', ['attribute' => __('portal.email', [],'en')], 'en'),
                ],
                'email.unique' => [
                   'nl' => __('validation.unique', ['attribute' => __('portal.email', [],'nl')], 'nl'),
                   'en' => __('validation.unique', ['attribute' => __('portal.email', [],'en')], 'en'),
                ]
            ];
        }
    }
    

    然后输出将如下所示:

    {
        "message":"The given data was invalid.",
        "errors":{
            "email":[
                {
                    "nl":"E-mailadres is verplicht.",
                    "en":"The email field is required."
                }
            ]
        }
    }
    

    以下是有关自定义消息的更多文档: https://laravel.com/docs/8.x/validation#specifying-custom-messages-in-language-files

    【讨论】:

    • 感谢您,但以上内容仅适用于这些规则和此类。我有几个扩展 ApiRequest 的类,它们都有不同的规则,并且语言是动态的。我正在尝试动态获取失败的验证并为每种语言生成错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    相关资源
    最近更新 更多