mardi 30 janvier 2018

Laravel customize file size error message and file size upload

how can i set a max size of image file to be upload on my laravel, and how can i customize the error message that will come return if the maximum size of the file was not met.

Here is my sample code but this doesn't work.

 $validator = Validator::make($request->all(), [            
            'fname' => 'required',
            'lname' => 'required',
            'mname' => 'required',
            'parentnum' => 'required|min:12|numeric',
            'prodsubcat' => 'required',
            'datetimepicker2' => 'required',
            'files.*' => 'image|size:2000|mimes:jpeg,png,jpg',

        ], [
            'fname.required' => 'First name is required.',
            'lname.required' => 'Last name is required.',
            'mname.required' => 'Middle name is required.',            
            'parentnum.required' => 'Parent contact number is required.',            
            'parentnum.min' => 'Parent contact number must be 12 digits.',                        
            'parentnum.numeric' => 'Parent contact number must be numeric.',            
            'prodsubcat.required' => 'Course is required.',
            'datetimepicker2.required' => 'Birthdate is required.',
            'files.*.image' => 'Selected file must be image.',
            'files.*.mimes' => 'Selected file must be jpeg,png,jpg.',
            'files.*.size' => 'Image size must not greater 2MB.'
        ]);
        if ($validator->fails()) {    
            return response()->json(['error'=>$validator->errors()->all()]);
        } else {
            $input=$request->all();
            $images=array();
            if($files=$request->file('files')){
            $i = 0;
            foreach($files as $file){
                $name=$file->getClientOriginalName();
                $file->move('image_files',$name);
                $images[]=$name;
                $i++;
                }
            } 

Any suggestions? i put max size from the code and also make it as max size in validation and the message i put doesn't come out, it will show error like this:The files.0 failed to upload.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire