Validating multiple files in array

后端 未结 4 1104
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 18:49

I need to validate multiple uploaded files, making sure they are of a specific type and under 2048kb. The below doesn\'t appear to check all files in the array \'files\' an

4条回答
  •  独厮守ぢ
    2020-12-15 19:18

    Please try this:

    public function fileUpload(Request $request) {
        $rules = [];
        $files = count($this->input('files')) - 1;
        foreach(range(0, $files) as $index) {
            $rules['files.' . $index] = 'required|mimes:png,jpeg,jpg,gif|max:2048';
        }
    
        $validator = Validator::make($request->all() , $rules);
    
        if ($validator->fails()) {
            return response()->json(array(
                'success' => false,
                'errors' => $validator->getMessageBag()->toArray()
            ) , 400);
        }
    }
    

提交回复
热议问题