Jump to:

23370 Posts in 18156 Topics by 2864 members

General Questions

SilverStripe Forums » General Questions » File Upload Problem with Custom Forms

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

Page: 1
Go to End
Author Topic: 941 Views
  • VicfromPerth
    Avatar
    Community Member
    8 Posts

    File Upload Problem with Custom Forms Link to this post

    Hi

    I am pretty new to Silverstripe and I need to create a custom form that uploads a file. I have gotten the form to save to the database no problems, but I keep getting a '[User Error] Bad RecordClassName '' and $baseClass not set' error when I try to upload the file.

    This is some of the code I am using:

    class FormsPage extends Page {
       static $db = array(
          'Date' => 'Date',
          'CompanyName' => 'Varchar(100)',
          'ContactName' => 'Varchar(100)',
          'Phone' => 'Varchar(20)',
          'Fax' => 'Varchar(20)',
          'Email' => 'Varchar(100)',
          'Event' => 'Varchar(200)',
          'Stand' => 'Varchar(50)',
          'Fascia' => 'Varchar(100)',
          'Quantity' => 'Int(10)',
          'FormType'=> 'Varchar(20)',
          'Centered'=> 'Varchar(1)',
          'Logo1'=> 'Varchar(1)',
          'Logo2'=> 'Varchar(1)',
          'Printed'=> 'Varchar(1)',
          'Comments'=> 'Text',
          'TextColour'=>'Varchar(50)',
          'FasciaColour' => 'Varchar(50)',
          'Artwork' => 'Varchar(200)' /*This is the field for the image, it is not required. I have tried setting it to File, but then I get errors when I run dev/build*/
                   
       );

    I have tried adding 'Artwork' => 'File' to the public static $has_one = array section, but this has not worked for me either.

    Then I create the form fields with the file field being

    new FileField("Artwork","Upload Artwork", "", "", "", "assets/Uploads/")

    I have also tried

    $uploadify = new FileUploadField('Artwork','Upload Artwork');
    $uploadify->setVar('buttonText','Upload File');

    But then I do not get the browse button to display (it shows the title 'Upload Artwork', but not the button).

    Then the code that processes the form is

    function FasciaAction($data, $form) {
          
                   if(isset($_FILES['Artwork'])) {
                      
                      // create the file from post data
                      $upload = new Upload();
                      $file = new File();
                                        
                      $upload->loadIntoFile($_FILES['Artwork'], $file);

                      // write file to form field
                      $submittedField->UploadedFileID = $file->ID;
                                     
                   }                           
                

          
          // Create a new forms object and load the form data into it
          $fascia = new FormsPage();
          $form->saveInto($fascia);

          $fascia->write();
          
          // Redirect to a page thanking people for registering
          Director::redirect('thank-you/');

          
       }

    If anyone can point me in the right direction as to what I am missing that would be awesome.

    Thanks!

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: File Upload Problem with Custom Forms Link to this post

    Try SimpleImageField instead - and if you have trouble with the upload of an image here is some code for that...

    $up =new Upload();
    $imgLogo = Object::create('Image');
    $up->loadIntoFile($data['Logo'], $imgLogo, 'Uploads');
    if($up->isError()) {
    //handle error here
    }

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