Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » [STILL UNSOLVED] Upload field fails on front end form

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

Page: 1
Go to End
Author Topic: 640 Views
  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    [STILL UNSOLVED] Upload field fails on front end form Link to this post

    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

  • tractorcow
    Avatar
    Community Member
    14 Posts

    Re: [STILL UNSOLVED] Upload field fails on front end form Link to this post

    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.

  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Re: [STILL UNSOLVED] Upload field fails on front end form Link to this post

    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

  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Re: [STILL UNSOLVED] Upload field fails on front end form Link to this post

    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

  • Optic Blaze
    Avatar
    Community Member
    162 Posts
  • Mo
    Avatar
    Community Member
    508 Posts

    Re: [STILL UNSOLVED] Upload field fails on front end form Link to this post

    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?

  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Re: [STILL UNSOLVED] Upload field fails on front end form Link to this post

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

    640 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.