【问题标题】:Laravel Backpack table field columns (json-array) validation rules not workingLaravel Backpack 表字段列(json-array)验证规则不起作用
【发布时间】:2020-07-11 20:14:36
【问题描述】:

我正在使用 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 班级有:

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 来验证所有名为 array 的字段 person 时,没有任何反应。

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

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

如果我转储输入数据,我可以看到函数 prepareForValidation 运行良好,但验证没有做任何事情。

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

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

但是,我想不那么繁琐,在ClientRequest 类中编码。

我认为我做得很好,但没有工作。

我在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的存储/更新功能中这样做:

$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' 丢失,某事无法正常工作。

【问题讨论】:

    标签: php laravel validation laravel-7 laravel-backpack


    【解决方案1】:

    改变

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

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

    并保留你的旧规则

    【讨论】:

    • 感谢您的回复,但这不起作用,因为验证需要一个“contact.person”字段名称永远不存在。我也试过。您还知道其他选择吗?
    • @Sakrow 你能发送一个你发送给验证的数组吗? dd($request->all())
    • @RamizKongulov 您好,我编辑了问题,添加了更多信息。我希望它对你有用。感谢您的宝贵时间。
    • @Sakrow contact.*.person - 如果您有一个多维数组,这是正确的表达式。你能发送一个验证错误吗? $validate = Validator::make($request->all(), ADD_RULE); dd($validate->messages())
    • @RamizKongulov 我用输出更新了它。某些东西不起作用,因为'contact.*.person' 没有出现
    猜你喜欢
    • 2020-03-01
    • 2018-11-09
    • 2020-03-25
    • 2018-04-23
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    相关资源
    最近更新 更多