Laravel多文件上传验证 [英] Laravel multiple file upload validation

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

问题描述

我目前正在使用表格.

I am currently working in a form.

我对多个文件上传验证存在一些问题.我的表单中只有一个字段可以上传多个文件.

I have some issue with multiple file upload validation. I have only one field in a form which allows multiple file upload.

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

这是我的验证,

$this->validate($request, [
    'file' =>'required',
    'file.*' => 'required|mimes:pdf,jpeg,png |max:4096',
],
 $messages = [
            'mimes' => 'Only PDF, JPEG, PNG are allowed.'
        ]
);

验证工作正常,但是我无法在刀片文件中显示错误消息.

The validation works perfectly but I am not able to display error messages in blade file.

这是我的尝试.

@if($errors->has('file'))
    <span class="help-block">
        <strong>{{$errors->first('file')}}</strong>
    </span>
@endif

如果没有上传文件,这是为了显示错误.

This is for displaying error if no file is uploaded.

假设我已经上传了以下文件,

Suppose I have uploaded following files,

abc.jpg
abc.html
abc.pdf

当哑剧类型验证引发错误时,我无法显示错误消息. 在这种情况下,由于验证在索引1处失败

When mimes type validation throws error I am not able to display error message. Here in this case, error is thrown as $error->first(file.1) since validation fails at index 1

根据上载的文件,该索引可以是任何索引,并且$error->first(file.*)也不能正常工作.

This index can be any index according to files uploaded and $error->first(file.*) doesn't work as well.

当我仅从表单中添加无效文件后显示所有错误时,我得到了这些错误.

When I display all error after adding invalid files only from form, I've got these errors.

 Only PDF, JPEG, PNG are allowed.
 The type field is required. 
 The number field is required.
 The expiry date field is required. 

任何人对此都有想法.感谢您的帮助.

Any one have idea about this. Any help is appreciated.

谢谢

推荐答案

这不是好方法,但对于我的情况来说很好.

This is not the good way, but it's fine for my case.

我有验证规则

'file' =>'required',
'file.*' => 'required|mimes:pdf,jpeg,png |max:4096',

然后,错误消息

'file.*' => 'Only PDF, JPEG, PNG are allowed.'

由于我只有一个文件上传字段,因此我刚刚在所有消息列表中检查了此错误消息,然后显示如下.

Since I have only one file upload field, I have just checked this error message in a list of all messages and then displayed as follows.

<input type="file" name="file[]" multiple="multiple">
@foreach($errors->all() as $error)
    @if($error=="Only PDF, JPEG, PNG are allowed.")
            <span class="help-block"><strong>{{$error}}</strong></span>
    @endif
@endforeach

谢谢大家,

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

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