使用IN规则进行Laravel数组验证

使用IN规则进行Laravel数组验证,laravel,Laravel,我正在尝试使用IN验证规则验证多选表单的值。尽管我的值与列表中的一个选项相同,但仍然会出现错误,即我的值不正确。我认为这必须是我在验证器中识别名称的方式(“步骤1”)。但是,我也使用了step1.,step1.0.,step1*。它将继续给出“无效响应”响应,该响应对应于错误中的 控制器 public function postQuestionDetailsStep1(Request $request) { if ($request->ajax()) { $

我正在尝试使用IN验证规则验证多选表单的值。尽管我的值与列表中的一个选项相同,但仍然会出现错误,即我的值不正确。我认为这必须是我在验证器中识别名称的方式(“步骤1”)。但是,我也使用了step1.,step1.0.,step1*。它将继续给出“无效响应”响应,该响应对应于错误中的

控制器

public function postQuestionDetailsStep1(Request $request)
{
    if ($request->ajax())
    {
        $step1 = $request->input('step1');

        $this->validate($request, [
            'step1' => 'required',
            'step1.0' => 'in:Less than $50,000,$50,000-$100,000,More than $100,000',
        ], [
            'step1.required' => 'You must choose one.',
            'step1.in' => 'Invalid response.',
        ]);
    }
}
看法


逗号在你的
规则中,在
规则中,弄乱你试图创建的数组。Laravel将如下读取您的阵列:

['Less than $50', '000', '$50', '000-$100', '000', 'More than $100', '000']
您可以将规则更改为以下内容,以解决逗号的问题:

in:Less than $50,000,$50,000-$100,000,More than $100,000'

确保
使用
规则
类:

use Illuminate\Validation\Rule;

您使用的是什么版本的Laravel?5.6,我相信……由于您的规则选项也有逗号,您是否尝试过在“少于50000美元”、“50000-100000美元”、“100000美元以上”中使用双引号:
?引用:在这样的验证中使用规则类让我很反感。然而,这是可行的。非常感谢。
in:Less than $50,000,$50,000-$100,000,More than $100,000'
Rule::in(['Less than $50,000', '$50,000-$100,000', 'More than $100,000']);
use Illuminate\Validation\Rule;