验证值,其中记录的另一列的值位于值数组中

时间:2019-08-16 15:40:11

标签: php laravel laravel-5

要验证的输入是一棵子类别有效的树,仅当其父类别也已选择时。

输入值如下:

$categories = [1,2,3];
//in database, each subcategory has column "category_id"
$sub_categories = [4,5,6]

验证

$this->validate([
 "sub_categories.*" => [
  "exists:sub_categories,id",
  //Rule for checking that the category_id for this subcategory
  //exists in the categories array.
 ]
])

是否有内置的laravel规则,或者我需要自己编写规则?

1 个答案:

答案 0 :(得分:0)

使用Rule

Validation

$this->validate([
    "sub_categories.*" => [
        "exists:sub_categories,id", 
        \Illuminate\Validation\Rule::in($categories),
    ],
])