laravel 验证整数数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42662579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:30:21  来源:igfitidea点击:

Validate an array of integers

laravellaravel-5laravel-5.3

提问by user7675955

Given an array of integers:

给定一个整数数组:

[1, 2, 3, 4, ...]

How can I use the validator to check if each of these exists in a table? Possible without a foreachloop?

我如何使用验证器来检查表中是否存在这些中的每一个?没有foreach循环可能吗?

$validator = Validator::make($request->all(), [
    'array' => 'required|exists:users,id'
]);

回答by patricus

Your validation as written should work. If the exists validation is used with an array, it will automatically use where infor the exists query.

您的书面验证应该有效。如果存在验证与数组一起使用,它将自动where in用于存在查询。

So, given your validation as you have written, the validation will get the count of the usersrecords where the idfield is in the list of ids provided by your arrayinput.

因此,根据您编写的验证,验证将获取usersid字段在您的array输入提供的 id 列表中的记录计数。

Therefore, if your arrayis [1, 2, 3, 4], it will get the count where users.id in (1,2,3,4), and compare that to the count of the elements in your arrayarray (which is 4). If the query count is >= the array count, validation passes.

因此,如果您array[1, 2, 3, 4],它将获得 where 的计数users.id in (1,2,3,4),并将其与array数组中元素的计数(即 4)进行比较。如果查询计数 >= 数组计数,则验证通过。

Two things to be careful about here: if the column you're checking is not unique, or if your array data has duplicate elements.

这里有两件事要小心:如果您要检查的列不是唯一的,或者您的数组数据是否有重复的元素。

If the column you're checking is not unique, it's possible your query count will be >= the array count, but not all ids from your array actually exist. If your array is [1, 2, 3, 4], but your table has four records with id 1, validation will pass even though records with ids 2, 3, and 4 don't exist.

如果您要检查的列不是唯一的,则您的查询计数可能 >= 数组计数,但并非数组中的所有 id 都实际存在。如果您的数组是[1, 2, 3, 4],但您的表有四个 ID 为 1 的记录,即使 ID 为 2、3 和 4 的记录不存在,验证也会通过。

For duplicate array values, if your array was [1, 1], but you only have one record with an id of 1, validation would fail because the query count will be 1, but your array count is 2.

对于重复的数组值,如果您的数组是[1, 1],但您只有一个 id 为 1 的记录,则验证将失败,因为查询计数将为 1,但您的数组计数为 2。

To work around these two caveats, you can do individual array element validation. Your rules would look something like:

要解决这两个警告,您可以进行单独的数组元素验证。您的规则如下所示:

$request = [
    'ids' => [1, 2, 3, 4],
];

$rules = [
    'ids' => 'required|array',
    'ids.*' => 'exists:users,id', // check each item in the array
];

$validator = Validator::make($request, $rules);

dd($validator->passes(), $validator->messages()->toArray());

Keep in mind that each element will be validated individually, so it will run a new query for each element in the idsarray.

请记住,每个元素都将单独验证,因此它将为ids数组中的每个元素运行一个新查询。

回答by manix

You can make your custom rule:

您可以制定自定义规则

public function validateArrayInt($attribute, $value, $parameters){  
    return array_filter(value, 'is_int')
}

Then:

然后:

$validator = Validator::make($request->all(), [
    'array' => ['required', 'array_int', 'exists:users,id']
]);

回答by Heartbeat

Try this to check whether your response array is json.

试试这个来检查你的响应数组是否是 json。

return response()->json($yourArr)->setEncodingOptions(JSON_NUMERIC_CHECK);