0
votes

I have problem when validating inputs. All input fields pass the validation process except image fields.

This is my file upload code in html:

<div class="control-group">
 <label class="control-label" for="fileInput"> Cover picture: </label>
    <div class="controls">
      {!! Form::file('cover') !!}
    </div>
</div>

And how I get data from view in controller:

$datas = array(
        'name' => Input::get('name'),
        'color' => Input::get('color'),
        'size' => Input::get('size'),
        'cover' => array('cover' => Input::file('cover'))
    );

And this is rules:

$rules = array(
        'name' => 'required',
        'color' => 'required',
        'size' => 'required',
        'cover' => 'required|mimes:jpeg,jpg,png|max:10000'
    );

And Validation facades`s make method:

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

As I mentioned earlier, all validation rules passed for input, but for image it gives me an error:

The cover must be a file of type: jpeg, jpg, png.

Now, how can I fix it?

1
Are you uploading a JPEG, JPG, or PNG file?ceejayoz
Yes, I am uploading JPEG, JPG or PNG. But it still gives me an error.memmedimanli
Are you including files => true on your Form::open call?ceejayoz
Yes, ceejayoz. I have included 'files' => true in Form::open. But still get an error.memmedimanli
Input::file('cover') should return an array of files without needing to make a secondary array under cover.. it is now multidimensional and not that expected.Cayce K

1 Answers

0
votes

I think you should approach this a little differently...

If you instead just create your function like so..

// The Illumniate/Http/Request version of Request in laravel
public function yourFunction(Request $request){
    ....
    $rules = ...;
    $validator = Validator::make($request->all(), $rules);
}

The expected format of the Validator is handled automatically because it returns it. And when it runs through you can handle everything the same you already have.


Since this is accepted. The way to actually fix it with the code above is to just remove the multi-dimensional array and just use Input::file('cover') as it returns an array on its own.