required_if工作不正确

时间:2017-03-26 21:57:49

标签: laravel laravel-5.4

我使用以下验证规则:

 'email' => 'required_if:phone-register,false|email|max:255|unique:users',

我传入的数据是:

array:6 [▼
  "_token" => "xPGUfppYirlLmSezTQduKZV4NXUAqf3I5DhrSJTZ"
  "email" => null
  "phone" => "514033390"
  "phone-register" => "true"
  "password" => "1234567"
  "password_confirmation" => "1234567"
]

尽管我的传入email为空,但email的验证仍然有效。为什么,如果我设置required_if:phone-register,false

完整的验证码:

 return Validator::make($data, [
            'email' => 'required_if:phone-register,"false"|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
            'phone-register' => 'required|boolean',
            'phone' => 'required_if:phone-register,"true"|numeric|min:6|max:15'
        ]);

现在我有:

 protected function validator(array $data)
    {

        $v =  Validator::make($data, [
            'password' => 'required|min:6|confirmed',
            'phone-register' => 'required',
            'phone' => 'required_unless:phone-register,true|numeric|min:6'
        ]);

        $v->sometimes('email', 'required|email|max:255|unique:users', function ($input) {
            return $input->phone-register === "false";
        });
    }

1 个答案:

答案 0 :(得分:1)

你可以add rules conditionally

$v = Validator::(....);

$v->sometimes('email', 'required|email|max:255|unique:users', function ($input) {
    return $input->phone-register === "false";
});