本文介绍了Laravel验证-如何检查给定数组中是否存在值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,好吧,我尝试了很多来自验证文档的规则,但都给出了相同的错误提示
So, okay i tried a lot of rules from validation docs but all give me same error saying
这是我添加数组的方式:
Here is how I add the array:
$this->validate($request,[
'employee' => 'required|in:'.$employee->pluck('id')->toArray(),
],[
'employee.in' => 'employee does not exists',
]);
关于如何实现这一目标的任何提示?
Any hint on how to achieve this?
我创建了一个自定义验证器,但仍然似乎无法传递数组
i created a custom validator but still passing array seems to be not possible
推荐答案
将数组放大为字符串并以逗号连接.
Implode the array as a string and join it on commas.
'employee' => 'required|in:'.$employee->implode('id', ', '),
这将生成验证器在进行in
比较时期望的正确的逗号分隔的字符串.
This will make the correct comma separated string that the validator expects when making an in
comparison.
修改
这仍然有效,但已不再是 Laravelesque 的使用方式.请参阅@nielsiano的答案.
This still works, but is not the Laravelesque way of doing it anymore. See the answer by @nielsiano.
这篇关于Laravel验证-如何检查给定数组中是否存在值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!