我的控制器中有自定义验证代码
代码
$validator = Validator::make($request->all(), [
'query' => 'required_without:fruits|string|min:2|max:50',
'page' => 'integer|max:10000000',
"fruits" => "required_without:query|array|min:1|max:100",
"fruits.*" => "required|string|min:2|max:50|distinct|exists:fruits,name"
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 422);
}
$query = $request->get('query');
$fruits = $request->get('fruits');
// How to replace this block with Laravel core validation rules
if(($query && mb_strlen($query) > 1) && (is_array($fruits) && count($fruits))) {
return response()->json([
'errors' => [
'You cannot use parameter "query" when parameter "fruits" already exists'
]
], 422);
}
当请求存在query
并且fruits
laravel 不返回错误时。当存在字段时,query
我不需要提交fruits
或何时必须fruits
提交query
。