I use Dingo API and FormRequest to validate HTTP API requests. I have those rules:
switch ($this->method()) {
case 'POST':
return [
'file' => 'required|mimes:pdf,png,jpeg,jpg',
'title' => 'required|unique:documents'
];
case 'PUT':
case 'PATCH':
return [
'id' => 'required',
'filename' => 'unique:documents,filename' . $this->route("id"),
'title' => 'unique:documents,title' . $this->route("id")
];
case 'DELETE':
return [
'id' => 'required'
];
}
However, I realised that I need to have two different rules in the case of POST. If a certain variable exists on the request, then I don't need the file to be required.
For example if collected
is true or exists on the object I send with HTTP request, then file
should be required. Otherwise not.
My fix at the moment is to split those two in different controllers, but I am not satisfied and I was looking for a tweak on the actual rules.
Any idea?