如果字段(assignee_id(是由用户提供的,则尝试验证该字段。
$validatedData = Validator::make($data, [
'title' => 'required|max:255',
'description' => '',
'assignee_id' => Rule::requiredIf(function () use ($request) {
return ($request->input('assignee_id') != '');})|'UUID',
'status' => [
'required',
Rule::in(['todo', 'done']),
],
'id' => 'UUID',
]);
$errors = $validatedData->errors();
if($validatedData->failed()) {
return response()->json($errors->all(), 400);
}
错误:
ErrorException: Object of class IlluminateValidationRulesRequiredIf could not be converted to int in file /Users/ankurjaiswal/Documents/php-learning/task-manger/app/Http/Controllers/TaskController.php on line 21
我假设您的意思是对assignee_id
使用2条规则。由于您希望使用Rule对象,因此您希望使用数组来定义此字段的规则:
'assignee_id' => [
Rule::requiredIf(function () use ($request) {
return ($request->input('assignee_id') != '');
}),
'UUID',
]
尽管您使用requiredIf
所做的是另一回事;正如@OMi Shah在评论中提到的那样。