[英]Laravel in_array validation rule
I have defined an array我已经定义了一个数组
$this->allslots=array('10:00:00', '10:10:00', '10:20:00', '10:30:00', '10:40:00', '10:50:00', '11:00:00', '11:10:00', '11:20:00', '11:30:00', '11:40:00', '11:50:00', '12:00:00', '12:10:00', '12:20:00', '12:30:00', '12:40:00', '12:50:00', '13:00:00', '13:10:00', '13:20:00', '13:30:00', '13:40:00', '13:50:00', '14:00:00', '14:10:00', '14:20:00', '14:30:00', '14:40:00', '14:50:00', '15:00:00', '15:10:00', '15:20:00', '15:30:00', '15:40:00', '15:50:00', '16:00:00', '16:10:00', '16:20:00', '16:30:00', '16:40:00', '16:50:00');
Now, I want to validate before saving data to database that, if a given input matches one of the value of the above array.现在,我想在将数据保存到数据库之前进行验证,如果给定的输入与上述数组的值之一匹配。 For this I'm doing
为此我正在做
$this->validate($request, [
'field' => 'required|in_array:$this->allslots',
]);
But, this returns validation error for every input.但是,这会为每个输入返回验证错误。 So, how can I do this?
那么,我该怎么做呢?
试试这个:
'field' => 'required|in:' . implode(',', $this->allslots),
This is a bit more semantic than the accepted answer:这比接受的答案更具语义:
use Illuminate\Validation\Rule;
$this->allslots=array('10:00:00', '10:10:00', '10:20:00', '10:30:00', '10:40:00', '10:50:00', '11:00:00', '11:10:00', '11:20:00', '11:30:00', '11:40:00', '11:50:00', '12:00:00', '12:10:00', '12:20:00', '12:30:00', '12:40:00', '12:50:00', '13:00:00', '13:10:00', '13:20:00', '13:30:00', '13:40:00', '13:50:00', '14:00:00', '14:10:00', '14:20:00', '14:30:00', '14:40:00', '14:50:00', '15:00:00', '15:10:00', '15:20:00', '15:30:00', '15:40:00', '15:50:00', '16:00:00', '16:10:00', '16:20:00', '16:30:00', '16:40:00', '16:50:00');
$request->validate([
'field' => [
'required',
Rule::in($this->allslots)
]
]);
I found a better solution.我找到了更好的解决方案。 The validate in_array expects the array to be one of the parameters in the request.
validate in_array 期望数组是请求中的参数之一。 The accepted answer will not work if you have commas in the array.
如果数组中有逗号,则接受的答案将不起作用。 To use the in_array without having to create a new rule you can simply do:
要使用 in_array 而不必创建新规则,您只需执行以下操作:
$this->allslots=array('10:00:00', '10:10:00', '10:20:00', '10:30:00', '10:40:00', '10:50:00', '11:00:00', '11:10:00', '11:20:00', '11:30:00', '11:40:00', '11:50:00', '12:00:00', '12:10:00', '12:20:00', '12:30:00', '12:40:00', '12:50:00', '13:00:00', '13:10:00', '13:20:00', '13:30:00', '13:40:00', '13:50:00', '14:00:00', '14:10:00', '14:20:00', '14:30:00', '14:40:00', '14:50:00', '15:00:00', '15:10:00', '15:20:00', '15:30:00', '15:40:00', '15:50:00', '16:00:00', '16:10:00', '16:20:00', '16:30:00', '16:40:00', '16:50:00');
$request['allslots'] = $this->allslots;
validate($request, [
'field' => 'required|in_array:allslots.*',
]);
Make sure you include the .* at the end确保在末尾包含 .*
Consider adding:考虑添加:
use Illuminate\Validation\Rule;
As found here: http://www.javaear.com/question/45810009.html在这里找到: http : //www.javaear.com/question/45810009.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.