laravel如何验证等于变量

时间:2017-08-18 18:17:35

标签: laravel validation laravel-5 laravel-5.4 laravel-validation

在laravel验证(注册)中我想将其中一个字段与php变量进行比较(它应该与之相等)

我该怎么做?

file

1 个答案:

答案 0 :(得分:2)

例如,您可以对名称字段执行此操作:

$variable = "something"
return Validator::make($data, [
    'name' => [
        'required',
        Rule::in([$variable]),
    ],
    'phone' => 'required|min:10|max:11|unique:users',
    'email' => 'required|email|max:255',
    'password' => 'required',
    'password_confirmation' => 'required',
    'user_captcha' => 'required'
]);

请记住导入规则类(使用Illuminate \ Validation \ Rule;)

您可以在https://laravel.com/docs/5.4/validation#rule-in

中获取更多信息

修改

根据@patricus的建议,这是一种更好的方法

$variable = "something"
return Validator::make($data, [
    'name' => 'required|in:'.$variable,
    'phone' => 'required|min:10|max:11|unique:users',
    'email' => 'required|email|max:255',
    'password' => 'required',
    'password_confirmation' => 'required',
    'user_captcha' => 'required'
]);