Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

[SOLVED] Can't see uploaded files in form submit handler when using UploadField in front-end form, with no underlying model.


Reply


2 Posts   306 Views

Avatar
vwd

Community Member, 159 Posts

20 February 2014 at 3:14pm

Edited: 20/02/2014 3:15pm

I'm working on a front-end form which has an UploadField. There is no underlying model as the form data and the uploaded files are sent out as an email with attachments - no need to store anything in the database.

What I'm finding though is that when the form is submitted, the files, although are uploaded to the specified upload folder, they are not listed in $data. Does anyone know if UploadField works without an underlying model?

The UploadField is set up as follows:

private function getUploadImagesFieldGroup() {
      $imageUploadField = new UploadField('UploadedImages', 'Upload Images');
      $imageUploadField->setCanAttachExisting(false);
      $imageUploadField->setCanPreviewFolder(false);
      $imageUploadField->relationAutoSetting = false;
      $imageUploadField->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif','tiff'));
      $imageUploadField->setFolderName('images/tmp-upload-images');
      $imageUploadField->setTemplateFileButtons('UploadField_FileButtons_ORS');
      
      $fg = new FieldGroup(            
         $imageUploadField
      );
      $fg->addExtraClass('upload ss-upload ss-uploadfield');    // had to add these classes because UploadField is added to FieldGroup
      $fg->setName('UploadImagesFieldGroup');
      
      return $fg;
   }

In my form submission handler:

public function formSubmissionHandler($data, $form) {
   // DEBUG
   var_dump($data['UploadedImages'];
   exit();
}

The output of the form submission:

array (size=6)
   'Files' =>
      array (size=1)
         0 => string '49' (length=2)
   'name' =>
      array (size=1)
         'Uploads' =>
            array (size=1)
               0 => string '' (length=0)
   'type' =>
      array (size=1)
         'Uploads' =>
            array (size=1)
               0 => string '' (length=0)
   'tmp_name' =>
      array (size=1)
         'Uploads' =>
            array (size=1)
               0 => string '' (length=0)
   'error' =>
      array (size=1)
         'Uploads' =>
            array (size=1)
               0 => int 4
   'size' =>
      array (size=1)
         'Uploads' =>
            array (size=1)
               0 => int 0

Any idea why this could be happening? Any suggestions or workarounds?

Thanks.
VWD.

Avatar
vwd

Community Member, 159 Posts

22 February 2014 at 3:54pm

Thanks to @simon_w who pointed out (on irc.silverstripe.com) that the '49' referred to the File ID of the already uploaded file.

I noticed that $data['UploadedImages'] was appropriately filled when doing a non-AJAX upload. But when doing AJAX uploads, the File IDs of the uploaded files are present in array.