Php 用于文本和多个文件的Laravel 5.2验证程序

Php 用于文本和多个文件的Laravel 5.2验证程序,php,file,validation,laravel,laravel-5.2,Php,File,Validation,Laravel,Laravel 5.2,我在同时验证多个文件和文本时遇到了一些问题。 当我验证整个请求时$request->all()文件规则不起作用。 'file'=>'必填项| mimes:png、jpeg、jpg、gif | max:3000'。 如果我只验证数组中的文件('file'=>$file),那么这个问题就会得到解决,但这样我就无法验证其他输入 我从互联网上获得了多文件部分,并为其他输入添加了我的部分,以下是我的功能: public function createNewPost(Request $request) {

我在同时验证多个文件和文本时遇到了一些问题。 当我验证整个请求时
$request->all()文件规则不起作用。
'file'=>'必填项| mimes:png、jpeg、jpg、gif | max:3000'
。 如果我只验证数组
中的文件('file'=>$file)
,那么这个问题就会得到解决,但这样我就无法验证其他输入

我从互联网上获得了多文件部分,并为其他输入添加了我的部分,以下是我的功能:

 public function createNewPost(Request $request) {
        $post = new Post;
        $post->user_id = Auth::user()->id;
        $post->title = $request->input('title');
        $post->body = $request->input('body');
        $post->status= "borrador";
        $post->save();
        $post->img = "/uploads/posts/".$post->id;
        $post->save();

        $files = Input::file('file');
        $file_count = count($files);
        $uploadcount = 0;
        foreach($files as $file) {
            $rules = array(
            'file' => 'required|mimes:png,jpeg,jpg,gif|max:3000',
            'title' => 'required|unique:posts|max:255',
            'body' => 'required'
            );
            $messages = [
                'title.required' => 'Sin titulo?',
                'body.required' =>  'No has escrito nada',
                'file.required' => 'Selecciona al menos 1 imagen.',
                'file.mimes' => 'No puedes utilizar ese tipo de imagen, intenta con (jpg/png/jpeg).',
                'file.max' => 'El total de imagenes no puede pesar mas de 3MB.'
            ];

            $validator = Validator::make(array('file'=> $file), $rules, $messages);
            if($validator->passes()){
                $destinationPath = 'uploads/posts/'.$post->id;
                //$filename = $file->getClientOriginalName();
                $filename = $uploadcount.".".$file->getClientOriginalExtension();
                $upload_success = $file->move($destinationPath, $filename);
                $uploadcount ++;
            }
        }
        if($uploadcount == $file_count){
            Session::flash('success', 'Upload successfully'); 
            return Redirect::to('/admin/post/new');
        } 
        else {
          return Redirect::to('/admin/post/new')->withInput()->withErrors($validator);
        }
    }

尝试此操作,并删除foreach文件循环:

$files = count($this->input('file')) - 1;
foreach(range(0, $files) as $index) {
    $rules['file.' . $index] = 'required|mimes:png,jpeg,jpg,gif|max:3000';
}

尝试此操作,并删除foreach文件循环:

$files = count($this->input('file')) - 1;
foreach(range(0, $files) as $index) {
    $rules['file.' . $index] = 'required|mimes:png,jpeg,jpg,gif|max:3000';
}