我得到了一个表单,它将许多要验证的输入字段发送到 Laravel 5 中的 Controller 中,其中有文件 name="arquivos[]"
<input>
字段。我想像我一样在一个步骤中解决所有验证问题,但它似乎失败了,因为它正在接收文件数组而不是单个文件。表格的代码是:
{!! Form::open(['route' => 'posts.store', 'enctype' => 'multipart/form-data']) !!}
...other inputs
{{ Form::file('arquivos[]', array('class' => 'novo-post-form-item', 'required'=>'','multiple' => '')) }}
{!! Form::close() !!}
在我的
posts.store
函数中:$this->validate($request, array(
'arquivos' => 'required|mimes:image/jpeg,image/png,image/gif,video/webm,video/mp4,audio/mpeg',
'assunto' => 'max:255',
'conteudo' => 'required|max:65535'
));
注意: 我不知道为什么在验证器中我必须指定没有
[]
的输入名称,但它是这样工作的......这个问题类似于我在堆栈溢出时发现的一些问题,但这次我问的是 Laravel 5 是否有解决方案。看起来,这个
validate
方法需要单个文件输入字段。提前致谢
最佳答案
请仔细阅读 Laravel 文档。真的很有帮助
要验证数组字段,您需要: https://laravel.com/docs/5.4/validation#validating-arrays 。
另外我认为你想使用 mimetypes
验证规则,因为 mimes
验证规则接受文件扩展名作为参数:https://laravel.com/docs/5.4/validation#rule-mimetypes
以及解决您的问题的方法:
$this->validate($request, array(
'arquivos.*' => 'required|mimetypes:image/jpeg,image/png,image/gif,video/webm,video/mp4,audio/mpeg',
'assunto' => 'max:255',
'conteudo' => 'required|max:65535'
));
关于php - 如何验证laravel 5中上传的多个文件的mime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44296880/