Adding the file name uploaded to the Laravel validation message

Viewed 1159

I have an array of files being uploaded. In the validation.php language file I've added custom error messages:

'custom' => [
    'images.*'      => [
            'dimensions' => 'Image ":file" must have a min-height of :min_height pixels and a min-width of :min_width pixels.',
            'image'      => 'You can only upload images to your gallery. The file ":file" was not an image.',
            'mimes'      => 'Your image ":file" was an invalid type. Images can only be of the following types: :mimes',
            'max'        => 'Max file size for each image is :max MB. Your image ":file" is too large.'
        ]
]

Since there are multiple files being uploaded, I would like to be able to specify which file the error message relates to when I display the validation errors.

@if($errors->has('images.*'))
    @foreach ($errors->get('images.*') as $message)
        <div class="help-block has-error">{{ head($message) }}</div>
    @endforeach
@endif

However, :file is not auto filled in the message and I don't know how to get it.

How would I go about doing this? Is there another placeholder or method I can use?

1 Answers

I have the same issue, what I try to do is to get names of files in an array and put it in custom error message depend on validation type in my example, I will get file name depend on image type (jpeg, png, jpg, SVG, BMP)

In controller file:

//...
public function store(Request $request)
    {
$rules = [        'imagefile' => 'required',
                  'imagefile.*' => 'image|mimes:jpeg,png,jpg,svg,bmp', // working on filse size custom message 
                 ];

       

        // To get name of non image file and display in it in error message 
        if($request->hasfile('imagefile')){
           
           $names = array(); // array for all names of files
          foreach($request->Bank_Logo as $file){
            

            // check file extension
            if($file->getClientOriginalExtension() != 'jpeg' &&
               $file->getClientOriginalExtension() != 'png' && 
               $file->getClientOriginalExtension() != 'jpg' &&
               $file->getClientOriginalExtension() != 'svg' &&
               $file->getClientOriginalExtension() != 'bmp'){

               $name= $file->getClientOriginalName (); // get file name 
               $names[] = $name; // set name of file to array 
              
            }
           
          }
        

          // check names array if it empty or not
          if(!empty($names)){
  
            /* but all names in custom error,message 
             * implode funaction to convert array to string 
             */
            $messages = [
                'imagefile.*image' => 'The ' . implode(" and ",$names) . ' not an image file.',
                'imagefile.*mimes' =>  'The ' . implode(" and ",$names) . ' must be a file of type: jpeg, png, jpg, svg, bmp.'
                  ];
          }         
        }              


               if(!empty($messages)){
                  $result = Validator::make($request->all(), $rules, $messages); // with custom message 
                   //dd($result);
               }
               else{ 
                   $result = Validator::make($request->all(), $rules); // without custom message
                    //dd('empty');
               }
        

        if($result->fails())
        {
            return back()->withErrors($result)->withInput($request->all());   
        }
       

        dd($request->all());

      //...
} 

In balde file

//...
<form>

label class="form-control-label mt-3">{{ __('imagefileLogo') }}</label>
                            <div class="file-loading">
                                <input id="imagefile" name="imagefile[]" type="file" multiple>
                            </div>

                            @if ($errors->has('imagefile'))
                            <span class="invalid-feedback" style="display: block;" role="alert">
                                <strong>{{ $errors->first('imagefile') }}</strong>
                            </span>
                            @endif

                            @if ($errors->has('imagefile.*')) <!--in case if we have multiple file-->
                            <span class="invalid-feedback" style="display: block;" role="alert">
                                <strong>{{ $errors->first('imagefile.*') }}</strong>
                            </span> 
                            @endif    

<form>  
//...

Same thing you can do with file size.

Also, I use [this plugin] for upload file input [this plugin]: https://plugins.krajee.com/file-input#top

Laravel version 5.8

I hope this was helpful to you. if anyone has a better way please discuss.

Related