dimanche 25 novembre 2018

Laravel - Image Array Validation gives an Error to nullable validate

I need to validate my image array as an image and specific image file extensions only. but my request validation to image WONT ALLOW me to use inser nullable values

For example I will add a content and dont want to add images. then the image should contain null that is why i need to have request validation as nullable. But in my experience null value is not allowed and it gives me error why? help me please

here is the error.

The Promotion Image must be an Image

here is my CONTROLLER

 public function store(Request $request)
    {
        $this->validate($request, [
            'promotion_image' => 'image|nullable|max:1999'
        ]);

            $promotion = [];

        if ($request->has('promotion_image'))
        {   
            //Handle File Upload

            foreach ($request->file('promotion_image') as $key => $file)
            {
                // Get FileName
                $filenameWithExt = $file->getClientOriginalName();
                //Get just filename
                $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
                //Get just extension
                $extension = $file->getClientOriginalExtension();
                //Filename to Store
                $fileNameToStore = $filename.'_'.time().'.'.$extension;
                //Upload Image
                $path = $file->storeAs('public/promotion_images',$fileNameToStore);
                array_push($promotion, $fileNameToStore);
            }

            $fileNameToStore = serialize($promotion);
        }
        else
        {
            $fileNameToStore='noimage.jpg';
        }

        if (count($promotion)) {
            $implodedPromotion = implode(' , ', $promotion);
            $promotionImage = new Promotion;
            $promotionImage->promotion_image = $implodedPromotion;
            $promotionImage->save();

            return redirect('/admin/airlineplus/promotions')->with('success', 'Image Inserted');
        }

        return redirect('/admin/airlineplus/promotions')->with('error', 'Something went wrong.');


        }

here is my VIEW

    {!! Form::open(['action'=>'Admin\PromotionsController@store', 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}
<div class="form-group">   
    <div class="table-responsive">  
        <table class="table table-bordered" id="dynamic_field">  
           <tr>  
              <td> </td>

              <td></td>
           </tr>  
        </table>  
        
    </div> 
</div>  
{!! Form::close() !!}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire