简体   繁体   English

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

[英]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.

相关问题 如何选择另一个值是数组的值? - How to select values where another value is an array? 如何在SQL中获取另一列等于值的一列中的值,并存储到PHP数组中? - How to get values in one column where another column equals a value with SQL, and store into an array in PHP? 对数组值与另一个数组的键匹配的数组进行排序 - Sort array where array value matches another array's keys MySQL语句从所有行获取列的值,其中另一列的值=指定的值 - MySQL statement to get a column's value from all rows where another column's value = a specified value 获取多维数组的值,其中键值在另一个数组中 - Getting the values of a multidimensional array where key value is in another array 选择在列中具有最小值并在另一列中具有特定值的记录 - Select Record with Min value in a column and with specific value in another column 获取特定数组值,其他值为“1”? - Get specific array value where another value is “1”? CodeIgniter:如何使用活动记录将一个列值移动到另一个具有 Where 条件的值 - CodeIgniter: How to move one column value to another with Where condition using active record 将值从一页传递到另一页,并从数据库中选择值,其中列是第一页的值 - Pass a value from one page to another and select values from database where a column is the first page value 将另一列的值与另一列的值相加 - sum another column's values where the another column is distinct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM