数组输入的自定义错误消息


Custom error messages for array input

在我的表单中考虑以下<input>数组:

 <input type="text" name="title[1]" value="">
 <input type="text" name="title[2]" value="">
 <input type="text" name="title[3]" value="">

数字(1,2,3)指的是不同的语言。 1 = 英语,2 = 德语等。

如何为输入数组添加自定义错误消息?


我在app/lang/en/validation.php中尝试了以下方法但没有成功:

<?php   
    return [
        'custom' => [
            'title.1' => [
                'required' => 'The english title is required.',
            ],
            'title.2' => [
                'required' => 'The german title is required.',
            ],
            'title.3' => [
                'required' => 'The italian title is required.',
            ],                                                                
        ],
    ];
?>

Laravel抛出默认错误消息,而不是使用我的自定义消息:

标题.1 字段是必填字段。
标题.2 字段为必填项。
标题.3 字段为必填项。

感谢您提供的任何帮助!


编辑:如果我像这样将消息传递给我的验证器,它会起作用:

$messages = array(
    'title.1.required' => 'The english title is required',
);
$validator = Validator::make($data = Input::all(), $rules, $messages);

但是我无法让它在app/lang/en/validation.php文件中工作。

通过使用以下方式,我得到了解决方案

在控制器中

        $input=array (
          'name' => 'pro 1',
          'barcode' => '2222',
          'vendors' => 
          array (
            0 => 
            array (
              'id' => 51,
              'name' => 'v1',
              'item_code' => 'khgjhgjhkhjgjhgjhkhjgjhgjhkhjgjhgjhvv',
            ),
            1 => 
            array (
              'id' => 43,
              'name' => 'v3',
              'item_code' => 'aerfaf132aw1d32aw1d32wad',
            ),
          ),
        ) 
        $validation = Product::validate($input);
          if ($validation != null && $validation != "" && $validation->fails()) {
            $breakline = $validation->messages()->all();
            $message = implode("<br> ", $breakline);
            Log::warning('Admin::ProductsController::store::' . $message);
            return Response()->json('', $message));
          }  

在模型中

public static function validate($data) {
  $rule = array(
    'name' => 'required|max:255',
    'barcode' => 'required|max:255',
    'vendors'=>'present|array|size:1,5',
    'vendors.*.item_code' => 'max:6'
  );
  $messages = array(
    'required' => ':attribute field is required.',
    'name.max' => ':attribute may not be greater than :max characters.',
    'barcode.max'=>':attribute may not be greater than :max characters.'
    'size'=>'only allowed one to five vendor.',
  );
  $data = Validator::make($data, $rule, $messages);
  $data->setAttributeNames(array(
    'name' => ucfirst('name'),
    'barcode' => ucfirst('barcode'),
    'vendors.*.item_code' => ucfirst('item code'),
  ));
  return $data;
}

它将显示

Item code may not be greater than 6.
Item code may not be greater than 6.

点表示法用于访问嵌套数组项,但您将其用于数组键。它期望title1是嵌套在一起的两个不同的数组键。这可能就是为什么您的自定义错误消息不匹配的原因。相反,请尝试以下操作:

return [
    'custom' => [
        'title' => [
            [ 1 => ['required' => 'The english title is required.']],
            [ 2 => ['required' => 'The german title is required.' ]],
            [ 3 => ['required' => 'The italian title is required.']],
        ],
    ],
];