Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel image validation just reloads page and does nothing

so the problem is I have image upload in my form, I did this before on other project and everything worked fine, but now for some reason I can’t use dd() command to see what is sent from <form>. Here is my controller code:

    

        $data = $request->validate([
        'model_id' => 'required',
        'part_name' => 'required',
        'part_category' => 'required',
        'make_years' => 'required',
        'price' => 'required',
        'identifier_number' => '',
        'part_image' => 'image|mimes:jpg,png,jpeg,gif,svg|max:2048'
    ]);

    dd($data);

And this is my blade part where image’s input is:


    <div class="form-row row-cols-1" style="margin-right: 0px;margin-left: 0px;">
                                            <label for="part_image" class="custom-file-upload btn btn-primary text-nowrap d-flex d-sm-flex d-md-flex d-lg-flex justify-content-center align-items-center justify-content-sm-center align-items-sm-center justify-content-md-center align-items-md-center justify-content-lg-center align-items-lg-center justify-content-xl-center align-items-xl-center">
    <i class="fa fa-cloud-upload"></i>Įkelti nuotrauką
    </label>
    <input id="part_image" name="part_image" type="file" hidden>
    @error('part_image')
    <span class="invalid-feedback" role="alert">
    <strong>{{ $message }}</strong>
    </span>
    @enderror
    
    </div>

So basically if I comment part_image validation in my controller, dd() function works, and I see all sent variables through POST, but if I try uncomment image validation, when I submit, page just reloads and I can’t even see any errors or dump message. What could be the problem? It’s probably some silly mistake, but I can find it.
I also have stated on my form enctype="multipart/form-data" so that’s not the case

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are not reaching dd() because you surely have validation errors, but you are not properly displaying them in your blade view. If you didn’t have any validation errors control flow would reach dd() and would print results inside controller. $request->validate(…) automatically redirects you back, so what you can do is to correctly display the errors in your blade view, preferable all at once to debug:

@if ($errors->any())
  <div class="alert alert-danger">
    <ul>
      @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
      @endforeach
    </ul>
  </div>
@endif
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading