Laravel 5.5 Validate multiple file upload

谁都会走 提交于 2019-12-06 06:21:38

问题


How do I validate multiple file uploads using only one validation on laravel?

$this->validate($request, [
    'project'           => 'required|exists:project_details,id',
    'location'          => 'exists:project_details,location',
    'plant_id'          => 'exists:project_details,plant_id',
    'capacity'          => 'required|max:20',
    'brief_description' => 'nullable|max:300',
    'incident_details'  => 'required|max:300',
    'other_comments'    => 'nullable|max:300',
    'attachments.*'     => 'required|file|mimes:xlsx,xls,csv,jpg,jpeg,png,bmp,doc,docx,pdf,tif,tiff'
]);

I'm trying to validate the attachments. Here's my form:

<input type="file" name="attachments[]" multiple>

回答1:


You can validate your file following way.

$input_data = $request->all();

$validator = Validator::make(
$input_data, [
'image_file.*' => 'required|file|mimes:xlsx,xls,csv,jpg,jpeg,png,bmp,doc,docx,pdf,tif,tiff'
],[
    'image_file.*.required' => 'Please upload an image',
    'image_file.*.mimes' => 'Only xlsx,xls,csv,jpg,jpeg,png,bmp,doc,docx,pdf,tif,tiff images are allowed',

]
);

if ($validator->fails()) {
    $messages = $validator->messages();
    return Redirect::to('/')->with('message', 'Your erorr message');
}


来源:https://stackoverflow.com/questions/48636473/laravel-5-5-validate-multiple-file-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!