【问题标题】:Laravel 5.5 Validate multiple file uploadLaravel 5.5 验证多个文件上传
【发布时间】:2018-07-16 03:07:00
【问题描述】:

如何在 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'
]);

我正在尝试验证附件。 这是我的表格:

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

【问题讨论】:

    标签: php validation laravel-5.5


    【解决方案1】:

    您可以通过以下方式验证您的文件。

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

    【讨论】:

    • @RalphVitto。我能理解你的问题。我将为这个问题添加一个解决方案。请检查我的答案。
    • @RalphVitto 你可以在 validate()->validate(); 结束时简单地调用 validate() 方法;
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 2017-11-05
    • 2014-06-30
    • 2018-06-13
    • 1970-01-01
    • 2017-02-12
    • 2020-11-29
    • 2016-02-25
    相关资源
    最近更新 更多