这是我的请求规则。
public function rules()
{
return [
"namespace" => "required|max:20",
"key" => "required|max:30",
"value" => "required",
"value_type" => "requied|in:string,integer",
];
}
如果“value_type”是“整数”,我只需要“value”字段中的整数。有没有办法验证它? Laravel 5.4
我找到了简单的解决方案
public function rules() { $cond = ''; if ($this->request->get('value_type') == 'integer') { $cond = '|integer'; } return [ "mf_id" => "required|integer", "resource" => "required|string", "res_id" => "required|integer", "value" => "required{$cond}", "value_type" => "required|in:string,integer", "parent" => "string", "parent_id" => "integer", ]; }
答案 0 :(得分:0)
$v = Validator::make($data, $this->rules());
然后你需要使用Conditional Adding Rules。
$v->sometimes(['value'], 'required|integer', function ($data) {
return is_numeric($input->value_type);
});
检查: -
https://laravel.com/docs/5.4/validation#conditionally-adding-rules