Laravel form validation (form request) with file upload field named documents[] Hi guys,
I am trying to set a rule in a form request file for documents which are uploaded my website.
public function rules()
{
return [
'documents[]' => 'max:8000',
];
}
but it doesn't seem to be working anyone have any ideas?
class="form-control"
type="file"
id="documents"
name="documents[]"
multiple
Thank you.
@frogeyedman Thanks it has helped but what if I want to check the whole array together rather than individual file uploads. Thanks
If you want to use the array itself you can do it like this:
$request->validate([ // will validate the array "name" => 'required|array|min:3', ]);
@nizam0786
you cannot just validate an empty array, what you can do is either loop on the FILES and validate each file if the format you want matches or do a dotted notation like
$request->validate([
'document.image' => ['required', 'max:8000']
]);
@jsdecena Hi thanks for your reply.
The array isn't empty multiple files can be added.
How would I loop through them in the form request file. I am not validating in the controller I am using a form request class so does that mean I have to override a method etc...
an example would be helpful if possible. Thanks
@nizam0786
if you want to validate all files you have to do it like this, it does not matter if you validating inside the controller or a form request class.
return [ 'documents.*' => 'max:8000', ];
this will validate all the uploaded documents inside the array, .* laravel handles it this way
@nizam0786 How about this?
https://gist.github.com/jsdecena/ab6532b52470d52de1aade79ac9b7686
<?php
use Illuminate\Http\UploadedFile;
class SomeController extends Controller {
// loop example
public function store(Request $request)
{
if($request->hasFile('files')) {
collect($request->file('files'))->each(function(UploadedFile $file){
$this->validateFiles($file);
$filesname = $file->store('folder-name', ['disk' => 'public']);
// Will output folder-name/randomstring.jpg (if you uploaded a jpeg)
dd($filename);
});
}
}
/**
*
* Validate the uploaded files
*/
private function validateFiles(UploadedFile $file)
{
// https://laravel.com/docs/5.6/validation#rule-file
$validator = Validator::make($request->all(), [
'file' => 'file,max:8000'
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
}
}
@frogeyedman I have tried that but no luck.
I have the following rule:
return [
'documents.*' => 'max:5000',
];
It does not allow me to upload a file larger than 5mb within the array however, it will allow me to still exceed 5mb thats with multiple files.
e.g. if I upload a 6mb file error will be returned but if I try and upload two 4mb files both will be uploaded. I want the limit for individual and the array as a whole to be 5mb.
hope that makes sense. Thanks
@jsdecena thanks for you reply will look into it.
@nizam0786 now it makes sense, i don't think laravel has a out of the box solution for that
Please sign in or create an account to participate in this conversation.