使用Laravel 4验证多个文件上传 [英] Validating multiple file uploads with Laravel 4

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

问题描述

如何在Laravel 4中验证上传的文件数组?我已经将其设置为允许多个文件的形式,并且已经测试了这些文件存在于Input :: file('files')数组中.但是如何验证每个文件?

How do I go about validating an array of uploaded files in Laravel 4? I've set it in the form to allow multiple files, and I've tested that the files exist in the Input::file('files') array. But how do I validate each file?

这是我尝试过的:

$notesData = array(
            'date' => Input::get('date'),
            'files' => Input::file('files')
    );


    // Declare the rules for the form validation.
    $rules = array(
            'date'  => 'Required|date_format:Y-m-d',
            'files'  => 'mimes:jpeg,bmp,png,pdf,doc'
    );

    // Validate the inputs.
    $validator = Validator::make($notesData, $rules);

    // Check if the form validates with success.
    if ($validator->passes())
    {
        // Redirect to homepage
        return Redirect::to('')->with('success', 'Validation passed!');
    }

    // Something went wrong.
    return Redirect::to(URL::previous())->withErrors($validator)->withInput(Input::all());

我希望Validator抱怨在数据数组中传递文件数组,但是即使我发送的文件是mp3,它也只是通过了验证.当我尝试上传多个文件时,出现了一个不相关的错误,即必须填写日期字段(尽管日期字段是自动填充的).

I expected Validator to complain about passing an array of files inside the data array, but it just passed validation even though the file I sent it was an mp3. When I tried to upload multiple files, it gave an unrelated error that the date field is required (though the date field was autopopulated).

我对Laravel很陌生.我该怎么做才能使它正常工作?

I'm pretty new to Laravel. What could I do to get this working?

更新:我发现问题的一部分是我修复的upload_max_filesize和post_max_size.我也尝试过这样动态地将文件添加到数组中:

UPDATE: I figured out part of the problem was my upload_max_filesize and post_max_size, which I fixed. I've also tried dynamically adding the files to the arrays as so:

$notesData = array(
            'date' => Input::get('date')
    );
    $i=0;
    foreach(\Input::file('files') as $file){
        $notesData['file'.++$i] = $file;
    }

    // Declare the rules for the form validation.
    $rules = array(
            'date'  => 'Required|date_format:Y-m-d'
    );
    for($j=1; $j<=$i; $j++){
        $rules['file'.$j] ='mimes:jpeg,bmp,png,doc'; 
    }

但是现在出现以下错误:

But now I'm getting the following error:

不允许对"Symfony \ Component \ HttpFoundation \ File \ UploadedFile"进行序列化

Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed

我迷路了.知道如何解决这个问题吗?

And I'm lost. Any idea how to fix this?

推荐答案

好吧,我不确定是什么原因导致了错误,但是在与验证器玩了一会儿之后,我发现我不能一次传递所有文件.相反,我创建了一个新数组,将每个文件与键"file0","file1"等关联.然后,将它们传递给验证器,并为每个文件设置规则(使用foreach循环).然后,验证程序按预期工作.但是,它的灵活性不足以满足我的需求,最终我使用了非Laravel解决方案.

Well, I'm not exactly sure what was causing the error, but after playing around a bit with the validator, I figured out that I couldn't just pass all the files at once. Instead, I made a new array that associated each file with keys 'file0', 'file1', etc. Then I passed these to the validator and set rules for each one (using a foreach loop). Then the validator worked as expected. Still, it wasn't flexible enough for my needs and I ended up using a non-laravel solution.

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

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