[英]Validate value where another column's value for the record is in array of values
Input to be validated is a tree where a subcategory is valid, only if its' parent category has been selected too. 要验证的输入是一棵树,其中子类别有效,仅当其父类别也已选择时也是如此。
The input values are as follows: 输入值如下:
$categories = [1,2,3];
//in database, each subcategory has column "category_id"
$sub_categories = [4,5,6]
Validation 验证方式
$this->validate([
"sub_categories.*" => [
"exists:sub_categories,id",
//Rule for checking that the category_id for this subcategory
//exists in the categories array.
]
])
Is there such a built in laravel rule or do I need to write the rule myself? 是否有内置的laravel规则,或者我需要自己编写规则?
Use the Rule
class 使用Rule
类
Validation
$this->validate([
"sub_categories.*" => [
"exists:sub_categories,id",
\Illuminate\Validation\Rule::in($categories),
],
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.