Laravel动态输入验证

时间:2018-06-29 16:20:43

标签: laravel laravel-5 eloquent

我正在开发一个应用程序,在其中创建动态输入。除一个用例外,验证工作正常。

验证规则在这里:

return [
         '*.id' => 'nullable|numeric',
         '*.name' => 'required|min:3|regex:/^[a-zA-Z0-9\s]+$/|unique:products,name,*.id,id,category_id,'.$this->route('id'),
         '*.quantity_available' => 'nullable|numeric|min:0',
         '*.quantity_sold' => 'nullable|numeric|min:0',
         '*.price'=> 'required|numeric|min:10',
         '*.sale_price' => 'required|numeric|min:10',
       ];

请查看以下发送的数据:

{id: 5, category_id: 3, name: "Test 1", description: null, quantity_available: null, quantity_sold: null, price:40, sale_price:35},
{id: 6, category_id: 3, name: "Test 2", description: null, quantity_available: null, quantity_sold: null,price:40, sale_price:35},
{id: "", name: "Test 3", quantity_available: "", quantity_sold: "", price: 40, sale_price: 35}

我希望产品名称按照category_id是唯一的,并且如果输入中存在ID,则在产品名称唯一验证时,该产品ID将被忽略。

0 个答案:

没有答案