【问题标题】:upload multiple images with system the validation not work使用系统上传多张图片验证不起作用
【发布时间】:2021-01-14 23:35:55
【问题描述】:

我尝试使用验证系统上传多个图像,即使我上传 jpeg 类型的图像,它也会给我验证系统错误图像必须是以下类型的文件:jpeg、jpg、png、gif、svg。

register.blade.php

<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
     @csrf
<div class="form-group" id="divim">
            <label>photos<span class="text-hightlight">*</span></label>
            <input class="form-control" type="file" name="images[]" value="{{ old('images') }}" multiple>
            @error('images')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
        
         <div class="col-md-6 offset-md-4">
            <button type="submit" class="btn btn-primary">
                {{ __('Register') }}
            </button>
         </div>
       </form>

RegisterController.php

protected function validator(array $data)
    {
        return Validator::make($data, [
     'images' => ['bail','required','mimes:jpeg,jpg,png,gif,svg','max:2048']
        ]);
    }

方法创建/存储

protected function create(array $data)
    { 
$dataim = array();
         if($request->hasFile('images'))
         {
            foreach($request->file('images') as $file) 
            {
                $namee = encrypt($file->getClientOriginalName()).'.'.$file->extension();
                //$name = encrypt($namee).'.'.$file->extension();
                $name = "profiles\\".$jdate->format('F').$jdate->year."\\".$namee;
                $file->storeAs("public\\profiles\\".$jdate->format('F').$jdate->year, $namee); 
                //$Annonce->images = "annonces\\".$jdate->format('F').$jdate->year."\\".time().'.'.$image->extension();  
                array_push($dataim,$name);
            }
         }
        $user->images=json_encode($dataim);
        $imm =$user->images;
return User::create([
 'images'       => $imm
        ]);
    }

【问题讨论】:

    标签: php laravel validation


    【解决方案1】:

    由于您想验证一个数组,您必须以不同的方式构建规则:

    return Validator::make($data, [
        'images' => ['bail', 'required', 'array', 'min:1'],
        'images.*' => ['bail', 'mimes:jpeg,jpg,png,gif,svg', 'max:2048'],
    ]);
    

    请参阅docs on validating arrays 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 2015-09-14
      • 2022-09-29
      • 2016-06-08
      • 2021-07-31
      • 2021-06-11
      相关资源
      最近更新 更多