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.

Form Questions /

[STILL UNSOLVED] Upload field fails on front end form


Reply


7 Posts   881 Views

Avatar
Optic Blaze

Community Member, 165 Posts

26 February 2014 at 1:15pm

Hi there,

I have built a front-end form with an upload field. At the moment if i upload a file with the same name it replaces the old one.
Form looks like this:

// ADD FILE FORM
public function AddForm() {
   return Form::create(
$this,
"AddForm",
FieldList::create(
            TextAreaField::create("Description", "Description")
            UploadField::create("MyFile","")
            ->setAllowedExtensions(array('doc','docx','txt','rtf','xls','xlsx','ppt','pptx','pps','csv','pdf'))
            ->setAllowedFileCategories('image', 'doc')
            ->setAllowedMaxFileNumber(1)
            ->setCanAttachExisting(false)
            ->setCanPreviewFolder(false)
            ->addExtraClass("required")
            ->setFolderName('jobfiles')
         ),
      FieldList::create(
            FormAction::create("AddAction","Add File")
               ->setStyle("primary"
            )
         )
    ) ;
}

I have looked at the docs and it seems that i need to add the following to my config.yml file:

---------------------config.yml-----------------------
Upload:
# Globally disables automatic renaming of files and displays a warning before overwriting an existing file
replaceFile: false

---------------------------------------------------------

I tried it, but it does not work (did do a dev build)
Is there another way to force the upload field to not overwrite my file names?

Thanks

Avatar
tractorcow

Community Member, 17 Posts

5 March 2014 at 5:00pm

Aha, this makes more sense to me now. :)

From what I can see, it seems that you should be getting a file exists warning on the frontend, asking you to proceed with the upload (and overwrite) or cancel.

I think what's wrong is that this dialogue doesn't work outside of the CMS, and is acting as though overwrite was always selected. This is obviously a problem, since we really want this field to work well in both the front and the back end.

For the mean time, just set setOverwriteWarning to false in your UploadField construction chain. This is probably the broken dialogue which is causing you trouble. Let me know if this solves your issue...

I'll do a bit of testing on the field tomorrow (probably, don't hold me to that) to see if I can come up with a better solution.

Avatar
Optic Blaze

Community Member, 165 Posts

27 March 2014 at 8:23am

Hi, tested a whole bunch of solutions...nothing worked. eventually i hacked the core upload class to just create a file with a random number on it.

in framework/filesystem/Upload.php on line 137:
------------------------------------------------------------------------

// Generate default filename
      
      $nameFilter = FileNameFilter::create();
      $filenamegenerator = date('Ymdhis')."-".rand(1,10000)."-";
      //$file = $nameFilter->filter($tmpFile['name']);
      $file = $nameFilter->filter($filenamegenerator.$tmpFile['name']);
      $fileName = basename($file);

      $relativeFilePath = ASSETS_DIR . "/" . $folderPath . "/$fileName";

Not pretty but it works

Avatar
Optic Blaze

Community Member, 165 Posts

27 March 2014 at 8:26am

Ran into a new problem. If all the javascript files are included that work on the back end, i cant get the file to upload. If i disable them, file upload happens, but there is no progress bar.

This is what the code looks like:

------------------------------------------------------

<?php

class JobFiles_Controller extends Page_Controller {
   
public static $allowed_actions = array (
      'Form',
      'view'
    );

   public function init() {
   parent::init();
   //Block custom jquery/javascript files
   Requirements::block('assets/_combinedfiles/myjs.js');
   Requirements::block('bootstrap_forms/javascript/bootstrap_forms.js');
   Requirements::block('bootstrap_forms/javascript/jquery.validate.min.js');
   
   //Load core SS jquery
   Requirements::javascript('framework/thirdparty/jquery/jquery.min.js');
   
   //BLOCK OTHER JAVASCRIPT FILES
      //Requirements::block('framework/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
      //Requirements::block('framework/javascript/i18n.js');
      //Requirements::block('framework/admin/javascript/ssui.core.js');
      //Requirements::block('framework/thirdparty/jquery-ui/jquery-ui.js');
      //Requirements::block('assets/_combinedfiles/uploadfield.js');
   
   }
   

// SET HOW URLS ARE HANDELED
   public static $url_handlers = array(
      '$Action/$ClaimID/$FilesID' => 'handleAction'
   );

//RETURNS CURRENT CLAIM DETAILS
   public function ClaimInfo() {
      if(!isset($this->urlParams['ClaimID'])) {
         $this->httpError(404);
         }
      else {
         return Claim::get()->byID($this->urlParams['ClaimID']);
         }
   }

// RETURNS JOBFILES RECORD
   public function Results() {
      $notes = JobFiles::get()->filter(array("ClaimsID"=> $this->ClaimInfo()->ID))->sort("Created","DESC");
      return $notes;
   }

// ADD FILE FORM
    public function Form() {
$fields = new FieldList(
new HiddenField('ClaimsID','', $this->urlParams['ClaimID']),
         new HiddenField('AuthorID','', Member::currentUserID()),
         new TextField('Description', 'Description'),
         
$field = new UploadField('MyFile', 'Upload File')
);
$field->setCanAttachExisting(false);
$field->setCanPreviewFolder(false);
$field->relationAutoSetting = false;
$actions = new FieldList(new FormAction('submit', 'Upload File'));
return new Form($this, 'Form', $fields, $actions, null);
}

public function submit($data, $form) {
$files = new JobFiles();
$form->saveInto($files);
$files->write();
Controller::curr()->redirectBack();
}
      
}

---------------------------------------------------------------------

This works...but i loose a whole bunch of finctionality

Avatar
Optic Blaze

Community Member, 165 Posts

21 April 2014 at 11:40pm

Anyone???

Avatar
Mo

Community Member, 514 Posts

6 August 2014 at 4:20am

Hi Optic,

You aren't loading the UploadField in dynamically via ajax are you? I had to do that recently and it was a MASSIVE pain because of issues with loading JS.

I often find issues with the progress bars on front end forms, although the latest version of 3.1 seems to be a lot more stable.

What version of SS are you running?

Avatar
Optic Blaze

Community Member, 165 Posts

8 August 2014 at 9:51am

I am currently running 3.1.2... will try upgrading and see what happens