Reputation:
I'm kinda stuck here on images validation in laravel. I have to validation the file input must be an image and for that I used the classical way of laravel validation but I don't why it is not working any clue?
User Controller
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());
}
Upvotes: 0
Views: 1860
Reputation: 1139
Just use this for validation. If you have to handle condition then go through step by step debuging.
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:5120',
'description' => 'required'
]);
Upvotes: 0
Reputation: 61
you can use as like this i think it is better.....
if($request->hasFile('profilePic')){
$rules = array(
'profilePic' => 'required | mimes:jpeg,jpg,png',
);
}
$validator = Validator::make($request->all(), $rules);
Upvotes: 0
Reputation: 35180
I would assume the reason your validation isn't working is because you adding the rule inside:
if($request->has('profilePic')){
This needs to be $request->hasFile('profilePic')
.
Hope this helps!
Upvotes: 2