Home > Mobile >  Laravel image validation just reloads page and does nothing
Laravel image validation just reloads page and does nothing

Time:05-05

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  style="margin-right: 0px;margin-left: 0px;">
                                            <label for="part_image" >
    <i ></i>Įkelti nuotrauką
    </label>
    <input id="part_image" name="part_image" type="file" hidden>
    @error('part_image')
    <span  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

CodePudding user response:

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 >
    <ul>
      @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
      @endforeach
    </ul>
  </div>
@endif
  • Related