1

The example is based on Laravel's registration.

I have added following to register.blade.php:

<div class="form-group row">
    <label for="file" class="col-md-4 col-form-label text-md-right">{{ __('Files') }}</label>

    <div class="col-md-6">
        <input type="file" id="files" name="files[]" multiple>
    </div>
</div>

The method in RegisterController looks like this:

protected function validator(array $data)
{
    $validator = Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'files.*' => ['required', 'file'],
    ]);

    dd($validator->errors());
}

I'm trying to upload a PDF and a DOC file.:

MessageBag {#236 ▼
  #messages: array:2 [▼
    "files.0" => array:1 [▼
      0 => "The files.0 must be a file."
    ]
    "files.1" => array:1 [▼
      0 => "The files.1 must be a file."
    ]
  ]
  #format: ":message"
}

Must be a file? These are files...

Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
Thomas Müller
  • 420
  • 4
  • 15
  • please post your form in the html file – Ahmed Nour Jamal El-Din Dec 15 '18 at 17:41
  • I tested this on a fresh installation with the basic register form: https://github.com/laravel/framework/blob/5.7/src/Illuminate/Auth/Console/stubs/make/views/auth/register.stub – Thomas Müller Dec 15 '18 at 18:02
  • You have to add `enctype="multipart/form-data"` to your form: https://stackoverflow.com/q/4526273/4848587 – Jonas Staudenmeir Dec 15 '18 at 18:08
  • You should check your php.ini too for ‍`upload_max_size` and `post_max_size`, if your file's size or sum of size of your files is bigger than those configs, php can not to upload them and laravel validation does not work properly – Morilog Dec 16 '18 at 05:53

2 Answers2

6

just add enctype="multipart/form-data" to your form:

<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
0

Try this :

protected function validator(array $data)
{
    $validator = Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'files.*' => ['required', 'mimes:doc,docx,pdf,txt'],
    ]);

    dd($validator->errors());
}
RoshJ
  • 461
  • 1
  • 6
  • 24