Validate file jpg - Laravel 5

3

I'm trying to use Validator to validate the upload file type but I do not know if the syntax is correct

    $data=Input::all();

    $file  = $data["picture_url"]; 
    $rules = array('picture_url' => 'required|max:10000|mimes:jpg');

    $validator = Validator::make($file, $rules);

    if ($validator->fails()) {
        echo "Arquivo inválido";
    }else{

    }

because you are giving the following error

  

ErrorException in Factory.php line 91: Argument 1 passed to   Illuminate \ Validation \ Factory :: make () must be of the type array,   object given, called in   C: \ Users \ ssilva \ Documents \ wispot-manager \ vendor \ laravel \ framework \ src \ Illuminate \ Support \ Facades \ Facade.php   online 219 and defined

Does anyone know how to solve it?

    
asked by anonymous 12.02.2016 / 19:13

2 answers

3

That's the problem. In Laravel, when you get an index of an input for an upload file, it returns an object of Symfony called Symfony\Component\HttpFoundation\File\UploadedFile\FilesBag (when it has more than one upload file) or a Symfony\Component\HttpFoundation\File\UploadedFile . >

In this case, you should pass array containing the following ['nome_do_indice_do_input' => 'valor_do_indice'] pair.

So instead of doing this:

$data = Input::all();

 $file  = $data["picture_url"];

Do this:

  $file  = Input::only('picture_url')

  Validator::make($file, $rules);

So, it will only get the index picture_url and will return a array containing key and field value. That way, the validation should work as expected.

You can also optionally keep your current code by changing only the line referring to Validator::make , like this:

 Validator::make(['picture_url' => $file], $rules);
    
12.02.2016 / 19:19
1

The $ file passed as argument in Validator has to be an array to compare with $ rules, since the way you are using the variables you are not picking up the file. you can do it as follows;

$data = Input::all();
$rules = array('picture_url' => 'required|max:10000|mimes:jpg');
$validator = Validator::make($data, $rules);
    
12.02.2016 / 19:16