0

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> {{ Form::file('promotion_image[]')}}</td>

              <td>{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle', 'id'=>'add','name'=>'add', 'style'=>'font-size:15px;']) }}</td>
           </tr>  
        </table>  
        {{Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit'])}}
    </div> 
</div>  
{!! Form::close() !!}

2 Answers 2

2

No need to add nullable attribute in the validation. just change your validation code like this

$this->validate($request, [
        'promotion_image.*' => 'image|max:1999'
    ]);

If you need user must add image input then you can use required validation rule other wise you don't need such thing.

Above code forces user to add file of type image or nothing at all.

I hope you understand and if any explanation needed, feel free to ask.

Sign up to request clarification or add additional context in comments.

7 Comments

what if I have 3 forms to insert and I dont want to add all the other 2 forms an image how can I submit that without any errors?
@obitouchiha Three forms ? or three image input fields
and also what if I want to update 3 images.. and i dont want to touch the other 2 images .. i only want to update one image. how can i set that without catching any error?
sir.. even tho i set it to 'image|max:1999' only it gives me an error :( The promotion image must be an image.
You can use some java script to mange that or manage all these things in the php
|
0

You need to validate it as an array:

$validator = Validator::make($request->all(), [
    'photos.profile' => 'required|image',
]);

Take a look laravel docs

you may want to combine it with sometimes which indicates that the current validation rules will apply only if the field is present.

6 Comments

hey sir. i think you are correct but it throws me an error here it is Validator' not found
it is just an example man, For you case i think it is promotion_image.* as you don't know how many images you my have. According to you comment in [what if I have 3 forms to insert and I dont want to add all the other 2 forms an image how can I submit that without any errors? ]. Also you have to import Validator to your class.
can you help me with the code pls? i dont quiet understand it :(
Validator is not found :(
At the top of your file add this use Illuminate\Support\Facades\Validator;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.