Laravel 5.4图像验证无效

时间:2017-05-19 06:00:10

标签: php laravel laravel-5

我有点被困在laravel中的图像验证。我必须验证文件输入必须是一个图像,为此我使用经典的laravel验证方式,但我不知道为什么它不工作任何线索?

用户控制器

public function createProfile(Request $request) {
    $phoneNumber=$request->phoneNumber;
    if (empty($request->except(['userId','token']))){
        $data= array(
            'nickName' => '',
            'profilePic' => '',
            'phoneNumber' => '',
            'userHeight' => '',
            'userWeight' => '',
            'userVertical' => '',
            'userSchool' => '',
            'homeTown' => '',
        );
        $this->setMeta("200", "Success");
        $this->setData("userDetails", $data);
        return response()->json($this->setResponse());
    }
    if($phoneNumber) {
        $validationData= array(
            'phoneNumber' => $phoneNumber,
        );
        $validationRules = array(
            'phoneNumber' => [
                'regex:/^[0-9]+$/',
                'min:10',
                'max:15',
                Rule::unique('users')->ignore($request->userId, 'userId'),

            ]
        );
        if($request->has('profilePic')){
            $validationData['profilePic'] = $request->profilePic;
            $validationRules['profilePic'] = 'image|mimes:jpeg,bmp,png';

        }
        $validator = Validator::make($validationData,$validationRules);
        if ($validator->fails()) {
            $errors = $validator->errors();
            if ($errors->first('phoneNumber')) {
                $message = $errors->first('phoneNumber');
            }  else if ($errors->first('profilePic')) {
                $message = $errors->first('profilePic');
            } else {
                $message = Constant::MSG_422;
            }
            $this->setMeta("422", $message);
            return response()->json($this->setResponse());
        }
    }
    $homeTown = $request->homeTown;
     $filename='';
    $profilePic=$request->file('profilePic');
    if(!empty($profilePic)) {
        $destinationPath = public_path() . '/uploads/users';
        $filename = "image_" . Carbon::now()->timestamp . rand(111, 999) . ".jpg";
        $profilePic->move($destinationPath, $filename);
    }

    $user = User::where('userId',$request->userId)->first();
    if($request->hasFile('profilePic')){
        $user->profilePic = $filename;
    }
    $user->nickName=$request->nickName;
    $user->phoneNumber=$request->phoneNumber;
    $user->userHeight=$request->userHeight;
    $user->userWeight=$request->userWeight;
    $user->userVertical=$request->userVertical;
    $user->userSchool=$request->userSchool;
    $user->homeTown=$homeTown;
    $user->save();
    $this->setMeta("200", "Profile Changes have been successfully saved");
    $this->setData("userDetails", $user);
    return response()->json($this->setResponse());
}

3 个答案:

答案 0 :(得分:2)

我认为你的验证不起作用的原因是你在里面添加规则:

if($request->has('profilePic')){

这必须是$request->hasFile('profilePic')

希望这有帮助!

答案 1 :(得分:0)

你可以像这样使用我认为它更好.....

     if($request->hasFile('profilePic')){                  
       $rules = array(
         'profilePic' => 'required | mimes:jpeg,jpg,png',
        );

    }      
   $validator = Validator::make($request->all(), $rules);

答案 2 :(得分:0)

只需使用它进行验证。如果你必须处理条件,那么一步一步地进行调试。

$this->validate($request, [
   'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:5120',
   'description' => 'required'

]);