Laravel 5.5验证多个文件上传 [英] Laravel 5.5 Validate multiple file upload

查看:532
本文介绍了Laravel 5.5验证多个文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅使用laravel上的一项验证来验证多个文件上传?

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>

推荐答案

您可以按照以下方式验证文件.

$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');
}

这篇关于Laravel 5.5验证多个文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆