Thomas Müller
Thomas Müller

Reputation: 420

Laravel 5.7.17 file validation issue

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...

Upvotes: 1

Views: 1351

Answers (2)

Rosh
Rosh

Reputation: 491

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());
}

Upvotes: 0

Ahmed Nour Jamal El-Din
Ahmed Nour Jamal El-Din

Reputation: 1011

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

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

Upvotes: 6

Related Questions