Laravel 背包 table 字段列(json-数组)验证规则不起作用

Laravel Backpack table field columns (json-array) validation rules not working

我正在使用 BackpackForLaravel,我的代码中有一个 table 类型字段:

$this->crud->addField([
    'label'           => 'Contact persons',
    'name'            => 'contact',
    'type'            => 'table',
    'entity_singular' => 'contact',
    'columns'         => [
        'person' => 'Person',
        'email'  => 'Email',
        'number' => 'Phone number',
        'desc'   => 'Description',
    ],
    'attributes'        => [
        'required' => true
    ],
    'max'               => 5,
    'min'               => 1,
    'wrapper'           => [
        'class' => 'form-group col-12'
    ],
    'tab' => 'Contact data',
]);

CRUD 工作正常,但是当我添加验证时,有些地方工作不正常。

我的 ClientRequest.php class 有:

public function rules()
    {
        return [
            'cif'                            => "required|spanish_tax_number|unique:clients,cif,{$this->cif},cif",
            'name'                           => 'required|min:3|max:255',
            'address'                        => 'required|json',
            'weekly_rest'                    => 'nullable|array',
            'description'                    => 'nullable|max:255',
            'leaving_date'                   => 'nullable|date',
            'leaving_date'                   => 'nullable|date',
            'image'                          => 'nullable|image|mimes:jpeg,png',
            'contact'                        => 'required|array',
            'contact.*.person'               => 'required|email'
        ];
    }

contact 字段验证效果很好,但是当我尝试使用 contact.*.person 验证所有名为 personarray 字段时,没有任何反应。

我使用函数prepareForValidation将字段json格式修改为array格式:

protected function prepareForValidation()
    {
        $this->merge([
            'contact' => json_decode($this->input('contact'))
        ]);
    }

如果我转储输入数据,我可以看到函数 prepareForValidation 运行良好,但验证并未执行任何操作。

我在这里阅读了数组验证:

我这样做了,并且有效(在 store/update 控制器函数中进行验证):

但是,我想在没有这种冗长的情况下做到这一点,所有代码都在 ClientRequest class.

我认为我做的很好,但不行。

我在 ClientRequest 中做错了什么?


编辑

request()->all() 内部 rules() 的输出,ClientRequest.php

的函数
array:15 [▼
  "_token" => "DGnGIZwertI6kupXwYgHr8as5MVbD2sPRlmbGJrK"
  "_method" => "PUT"
  "http_referrer" => "http://127.0.0.1:8000/admin/client?active=1"
  "id" => "21"
  "cif" => "123456789F"
  "name" => "Paco Porras SL"
  "address" => "Useful address, 1"
  "weekly_rest" => array:3 [▼
    0 => "2"
    1 => "3"
    2 => "4"
  ]
  "description" => "qweqwe"
  "discharge_date" => "2020-07-08"
  "leaving_date" => null
  "image" => null
  "contact" => "[{"person":"123123123","email":"sss.ssstyytrytr","number":"qwewqe","desc":"qweq"},{"person":"123123123","email":"123123","number":"123123","desc":"123213"}]"
  "current_tab" => "datos-de-empresa"
  "save_action" => "save_and_edit"
]

prepareForValidation后的输出:

array:15 [▼
  "_token" => "DGnGIZwertI6kupXwYgHr8as5MVbD2sPRlmbGJrK"
  "_method" => "PUT"
  "http_referrer" => "http://127.0.0.1:8000/admin/client?active=1"
  "id" => "21"
  "cif" => "123456789F"
  "name" => "Paco Porras SL"
  "address" => "Useful address, 1"
  "weekly_rest" => array:3 [▼
    0 => "2"
    1 => "3"
    2 => "4"
  ]
  "description" => "qweqwe"
  "discharge_date" => "2020-07-08"
  "leaving_date" => null
  "image" => null
  "contact" => array:2 [▼
    0 => {#1557 ▼
      +"person": "123123123"
      +"email": "sss.ssstyytrytr"
      +"number": "qwewqe"
      +"desc": "qweq"
    }
    1 => {#1556 ▼
      +"person": "123123123"
      +"email": "123123"
      +"number": "123123"
      +"desc": "123213"
    }
  ]
  "current_tab" => "datos-de-empresa"
  "save_action" => "save_and_edit"
]

编辑 2

我在 ClientController.php 的 store/update 函数中这样做:

$validate = Validator::make($request->all(), [
            'contact'                          => 'required|array',
            'contact.person'                   => 'required|string',
            "contact.*.person"                 => 'required|string',
        ]);

dd($validate->messages());

输出为:

Illuminate\Support\MessageBag {#1520 ▼
  #messages: array:2 [▼
    "contact" => array:1 [▼
      0 => "El campo contact debe ser un array."
    ]
    "contact.person" => array:1 [▼
      0 => "El campo contact.person es obligatorio."
    ]
  ]
  #format: ":message"
}

由于 'contact.*.person' 缺失,部分功能运行不正常。

改变

'contact' => json_decode($this->input('contact'))

'contact' => json_decode($this->input('contact'), true)

并保留你的旧规则