【问题标题】:Format validation error message as nested associative array将验证错误消息格式化为嵌套关联数组
【发布时间】:2018-11-09 12:20:39
【问题描述】:

我有一个 Json 数据对象,如下所示

{
  "name": "something",
  "location": {
    "city": "some where",
    "country": "some where",

  }
}

用于验证请求的规则是

[
    'name' => 'required',
    'location.city' => 'required',
    'location.country' => 'required'
]

返回类似

的错误信息
{
  "name": [
    "The name field is required."
  ],
  "location.city": [
    "The location.city field is required."
  ],
  "location.county": [
    "The location.country field is required."
  ]
}

如何将错误消息格式化为像请求一样的嵌套数组。

{
  "name": [
    "The name field is required."
  ],
  "location": {
    "city": [
      "The city field is required"
    ],
    "country": [
      "The country field is required"
    ]
  }
}

任何可用的默认方法? 我正在使用Illuminate\Foundation\Http\FormRequest

【问题讨论】:

    标签: laravel validation laravel-5.7


    【解决方案1】:

    在您的情况下,您需要自己构建错误消息。您仍然可以使用 ressources/lang/en/validation 消息文件中的默认消息。

    $validator = Validator::make($request->all(), [
        'name' => 'required',
        'location.city' => 'required',
        'location.country' => 'required'
    ]);
    
    if ($validator->fails()) {
        return response()->json($yourOwnFormat,422);
        //you can use $validator->errors() to build it
    }
    

    【讨论】:

      【解决方案2】:

      对于那些正在寻找解决方案的人这就是我实施的方式

      <?php 
      namespace App\Http\Requests;
      
      use Illuminate\Contracts\Validation\Validator;
      use Illuminate\Foundation\Http\FormRequest;
      use Illuminate\Http\Exceptions\HttpResponseException;
      
      class UserStoreRequest extends FormRequest
      {
          public function rules()
          {
              return [
                  'name' => 'required',
                  'location.city' => 'required'
                  'location.country' => 'required'
              ];
          }
          public function attributes()
          {
              return [
                  'location.city' => 'City'
                  'location.country' => 'Country'
              ];
          }
          protected function failedValidation(Validator $validator)
          {
              $errors = $validator->errors()->getMessages();
              $errors_formated = array();
              foreach ($errors as $key => $value) {
              array_set($errors_formated, $key, $value);
              }
              throw new HttpResponseException(response()->json(['error' => $errors_formated], 422));
          }
      }
      

      $validator-&gt;errors()-&gt;getMessages() 的结果就像array_dot() 辅助函数结果一样。所以我做了array_dot()的相反,也把我的属性名改成了漂亮的名字

      【讨论】:

      • 我收到错误:未定义的函数数组集
      【解决方案3】:

      是吗? Laravel Documentation- Customizing the Error Messages

      public function messages()
      {
          return [
              'location.city' => 'The city field is required',
              'location.county'  => 'The county field is required',
          ];
      }
      

      【讨论】:

      • 它只会改变像The city field is required这样的消息部分我需要改变密钥location.city。请看我的回复样本
      猜你喜欢
      • 2019-04-15
      • 2016-01-06
      • 2023-01-21
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 2017-12-28
      • 1970-01-01
      • 2016-05-24
      相关资源
      最近更新 更多