Laravel in_array 验证规则
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38883289/
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
Laravel in_array validation rule
提问by Maulik Gangani
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?
但是,这会为每个输入返回验证错误。那么,我该怎么做呢?
回答by Pawel Bieszczad
Try this:
尝试这个:
'field' => 'required|in:' . implode(',', $this->allslots),
Does that give you the expected result?
这会给你预期的结果吗?
回答by Maulik Gangani
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)
]
]);
回答by Arturo Alvarado
I found a better solution. The validate in_array expects the array to be one of the parameters in the request. 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:
我找到了更好的解决方案。validate in_array 期望数组是请求中的参数之一。如果数组中有逗号,则接受的答案将不起作用。要使用 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
确保在末尾包含 .*