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 from controller not working

I’m trying to create a validator to my form input, to only accept images. I’ve searched here on stack and other forums, but all the answers that i got didn’t work for me.. Most of the answers where syntax erros of the lack of enctype="multipart/form-data".. but that is not the case here, aparently.. Here is my form and my controller:

Form:

<form method="post" action="{{url('file')}}" enctype="multipart/form-data">
    @csrf

    <div class="input-group hdtuto control-group lst increment" >
   
        <input type="file" name="filenames[]" class="myfrm form-control">
   
        <div class="input-group-btn"> 
   
            <button class="btn btn-success btn-block" type="button" style="width: 100px !important"><i class="fldemo glyphicon glyphicon-plus" ></i>Add</button>
   
        </div>
   
    </div>
   
    <div class="clone hide">
   
        <div class="hdtuto control-group lst input-group" style="margin-top:10px">
   
            <input type="file" name="filenames[]" class="myfrm form-control">
   
            <div class="input-group-btn"> 
   
                <button class="btn btn-danger btn-block" type="button" style="width: 100px !important"><i class="fldemo glyphicon glyphicon-remove"></i> Remove</button>
   
            </div>
   
        </div>
   
    </div>

    <button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>

</form>

<?php

Controller:

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

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\File;

class FileController extends Controller
{
/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('create');
}

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $this->validate($request, [
            'filenames' => 'mimetypes:jpeg,png,jpg',
            'filenames.*' => 'required'
          ],$messages = [
            'required' => 'The :attribute field is required.',
            'mimes' => 'Apenas jpeg, png, png são permitidos.'
    ]
    );

    $files = [];
    if($request->hasfile('filenames'))
     {
        foreach($request->file('filenames') as $file)
        {
            $name = time().rand(1,100).'.'.$file->extension();
            $file->move(public_path('files'), $name);  
            $files[] = $name;  
        }
     }

     $file= new File();
     $file->filenames = $files;
     $file->save();

    return back()->with('success', 'Data Your files has been successfully added');
}
}

I’ve tried to change the validation to this: 'mimes:jpeg,png |max:4096, required', , but still not working..

>Solution :

Use like this

$rules = array(
     'filenames' => 'required|array',
     'filenames.*' => 'image|mimes:jpg,jpeg'
);
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