Ad

OctoberCMS Form File Upload | Invalid Argument Supplied For Foreach()

I'm fairly new to OctoberCMS & Laravel. I've been setting up a contact form everything works perfectly besides adding multiple file attachments. I've had a search online and haven't been able to find a solution for why my form won't send.

The error in question is "invalid argument supplied for foreach() on line 26"

Line 26 is the first foreach loop

I've attached the form html markup below as well as the controller for the form.

PHP Code

<?php
namespace Bprint\Contact\Components;
use Cms\Classes\ComponentBase;
use Input;
use Mail;
class ContactForm extends ComponentBase{
    public function componentDetails(){
        return [
            'name' => 'Contact Form',
            'description' => 'Bprint Contact Form'
        ];
    }
    public function onSend(){
        $vars = ['fieldName' => Input::get('fieldName'), 'fieldCompany' => Input::get('fieldCompany'), 'fieldEmail' => Input::get('fieldEmail'), 'fieldUpload' => Input::file('fieldUpload'), 'fieldContent' => Input::get('fieldContent')];
        $attachments = [];
        foreach($vars['fieldUpload'] as $image) {
            $attachments[] = $image;
        }
        Mail::send('bprint.contact::mail.message', $vars, function($message) use ($attachments) {  
            foreach($attachments as $attachment) {
                $params = ['as' => $attachment->getFilename()/*, 'mime' => $mime*/];
                $message->attach($attachment->getLocalPath(), $params);
            }
            $message->to('REDACTED', 'BlueprintCMS');
            $message->subject('Blueprint Website | Contact Form"');
        });
    }
}

HTML form Markup

    <form data-request="onSend" data-request-loading="#loading" data-request-error="alert('There was an error sending your message. Please try again shortly, if error persits please email us at [email protected]')" data-request-success="alert('Your message has been sent.')" class="bpsite-form floating-labels">
      <fieldset>
          <div class="icon">
                  <label class="bpsite-label" for="bpsiteName">Name</label>
                  <input name="fieldName" class="user" type="text" id="bpsite-name" required>
          </div>
          <div class="icon">
              <label class="bpsite-label" for="bpsite-company">Company</label>
              <input name="fieldCompany" class="company" type="text" id="bpsite-company">
          </div>
          <div class="icon">
              <label class="bpsite-label" for="bpsite-email">Email</label>
              <input name="fieldEmail" class="email" type="email" id="bpsite-email" required>
          </div>
          <input type="file" name="fieldUpload[]" multiple>
      </fieldset>
      <fieldset>
          <div class="icon">
              <label class="bpsite-label" for="bpsite-textarea"></label>
        <textarea name="fieldContent" class="message" id="bpsite-textarea" required></textarea>
          </div>
          <div>
              <button id="bpsite-button" style="display: none;" class="btn btn-primary form-btn" type="submit">Send</button>
              <label for="bpsite-button">
                <div class="box">
                      <span data-text="S">S</span>
                      <span data-text="E">E</span>
                      <span data-text="N">N</span>
                      <span data-text="D">D</span>
                  </div>
              </label>
        <div id="loading" style="display: none;">
          <div class="col-2 col-sm-2 col-md-2 col-lg-2 col-xl-2">
            <img class="img-fluid" src="{{ 'loader.gif'|media }}" alt="Loading..." />
          </div>
          <div class="col-10 col-sm-10 col-md-10 col-lg-10 col-xl-10">
            <div class="alert alert-info">
              <p class="body-text">Your message is being sent...</p>
            </div>
          </div>
        </div>
          </div>
      </fieldset>
    </form>

Thanks in advance to anyone who can help me out here!

Kind regards, Jack

Ad

Answer

You are using ajax framework of OctoberCMS and you want to upload file attachment with this form then you need to add one more attribute data-request-files to form element.

Your form element will look like this:

<form data-request="onSend"
    data-request-files
    data-request-error="alert('There was an error sending your message. Please try again shortly, if error persits please email us at [email protected]')"
    data-request-success="alert('Your message has been sent.')"
    class="bpsite-form floating-labels">

and it will start working.

Ad
source: stackoverflow.com
Ad