How to trigger an Upload Validation Message?
I am currently trying to make my Avatar Upload more efficient and intelligent. Even though I added a validation rule of max:800 (is there any better solution?) my Controller uploaded also bigger files (examples provided below)
Is there a way to somehow trigger the several rules in order to see how the related messages will be displayed within the Users interface and to test if the validators work by testing multiple images?
UploadController
public function uploadAvatar(Request $request){
if($request->hasFile('avatar')){
$user = Auth::user();
$input_data = $request->all();
// dd($input_data);
$validator = Validator::make(
$input_data, [
'avatar.*' => 'required|mimes:jpg,jpeg,png,bmp|max:2000'
],[
'avatar.*.required' => 'Please upload an image',
'avatar.*.mimes' => 'Only jpeg,png and bmp images are allowed',
'avatar.*.max' => 'Sorry! Maximum allowed size for an image is 5MB',
]
);
if ($validator->fails()) {
return response()->json(array(
'success' => false,
'errors' => $validator->errors()->toArray()
), 400);
}
$image = $request->file('avatar');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('uploads/avatars/'. $filename);
Image::make($image)->resize(300,300)->save($location);
$user->avatar = $filename;
$user->save();
}
return redirect()
->route('user.profile.editavatar')
->with('info', 'Your avatar has been uploaded.');
}
Should I outsource my rules/messages within an e.g. UploadRequest?
Tested Avatars (Both uploaded)
me.jpg: size: 6815703 (6,8 MB)
// pathname: "/tmp/php8yU31W"
profile.jpg: size: 244832 (0,24 MB)
// pathname: "/tmp/phpTK9NAp"
I added the dd($input_data)'s pathnames which is a temporary folder. So my question is, if I could use the temporary name, to display the chosen validated Image (Preview Idea) for the User, before uploading the image. (primarily for multiple upload case).
Thank you in advance.
Please or to participate in this conversation.