Laravel中的多文件上传验证 [英] Multiple file upload validation in laravel

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

问题描述

我有多个输入字段,每个字段都有多个文件上传.

I have multiple input fields with multiple file upload for each field.

<?php for ($i = 0; $i < $total; $i++)
   {
?>
<div class="col-md-3 addedClass">
     <label>Vehicle Images</label>
     <input type="file" name="vehicle_image[{{$i}}][]" multiple="multiple">
     @if($errors->has('vehicle_image'))
          <span class="help-block">
              <strong>{{$errors->first('vehicle_image')}}</strong>
          </span>
     @endif
</div>
<?php } ?>

我在请求中有这样的文件:

I have got files in the request like this:

"vehicle_image" => array:2 [▼
    0 => array:2 [▼
      0 => "citizenship.jpg"
      1 => "logo_vehicle.png"
    ]
    1 => array:2 [▼
      0 => "ae backend.jpg"
      1 => "logo_vehicle.png"
    ]
  ]

在这种情况下,我有两个带有2/2文件的输入字段.当我尝试像这样验证only images的mime类型时:

In this case, I have two input fields with 2/2 files. When I have tried to validate mime type for only images like this:

$this->validate($request,[
           'vehicle_image' => 'mimes:jpeg,png,bmp,tiff |max:4096'
       ],$messages[
            // error messages 
      ]);

我遇到以下错误:

FatalThrowableError in ReservationController.php line 67: Cannot use [] for reading

有人可以告诉我上述代码有什么问题吗? 建议表示赞赏.

Can someone tell me what is wrong with the above code ? Suggestions are appreciated.

推荐答案

尝试删除空白的$ messages数组,并为请求的输入调用函数all().

Try removing the blank $messages array and calling the function all() for the inputs on the request.

$rules = [
    'vehicle_image' => 'mimes:jpeg,png,bmp,tiff |max:4096'
];
$this->validate($request,$rules);

要显示默认错误消息,您将使用类似以下内容引发异常:

To display the default error message you would throw an exception using something like:

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
        return redirect('VIEWPATH.VIEWNAME')->withErrors($validator)->withInput();
    }

然后在刀片视图或模板中显示如下错误:

Then to display the errors you can have something like the following in your blade view or template:

@if (count($errors) > 0)
  <div class="alert alert-danger alert-dismissable fade in">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <h4>
      <i class="icon fa fa-warning fa-fw" aria-hidden="true"></i>
      <strong>Error!</strong> See error messages...
    </h4>
    <ul>
      @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
      @endforeach
    </ul>
  </div>
@endif

@if(session()->has('errors'))
    <div class="alert alert-danger fade in">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        <h4>Following errors occurred:</h4>
        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

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

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