Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » SS3 UploadField

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

Page: 1
Go to End
Author Topic: 3075 Views
  • tfliam
    Community Member
    20 Posts

    SS3 UploadField Link to this post


    I have 2 questions regarding using Uploadfield in frontend to upload images:

    1) By default, uploadfield will upload images as 'File'. I have a custom class called "ProfilePhoto" that extends "Image" which extends "File". Is there a way to configure uploadfield so that it upload images as "ProfilePhoto" ?

    2) The "ProfilePhoto" has a Has-One relationship to "Member". How do I configure the UploadField so that after the image is uploaded, it will auto-detect and update the relationship.

  • Andre
    Community Member
    126 Posts

    Re: SS3 UploadField Link to this post

    Hi there,

    even if it is not that old, let me wake up this thread.

    I have the same problem with the UploadField.

    I want to use it in a frontendform instead of the SimpleImageField, that I have used in the previous Silverstripe Versions.

    First, is there any way, to configure the UploadField, to hide the "From Files" Button.

    Next: If I make the UploadField Required, it returns the UploadField always with the error message, that it was not filled.

    Also, it seems, that I can't write it's content to the DataObject by $form->saveInto(DATAOBJECT).

    Here is a Piece of code that I use:

    // CRUD
    public function MitarbeiterAddForm(){

    $data = Session::get("FormInfo.Form_MitarbeiterAddForm.data");

    $avatarField = new UploadField('Avatar', 'Mitarbeiter Photo');
    $avatarField->allowedExtensions = array('jpg', 'gif', 'png');
    //print_r($avatarField->getAttributes()); die();

    // Create fields
    $fields = new FieldList(
    new TextField('Name', 'Name'),

    // Create actions
    $actions = new FieldList(
    new FormAction('doAdd', 'Mitarbeiter anlegen')

    $requiredFields = new RequiredFields(

    $form = new Form($this, 'MitarbeiterAddForm', $fields, $actions, $requiredFields);

    return $form;

    public function doAdd($data, $form){

    $errors = false;

    if($Mitarbeiter = DataObject::get_one('Mitarbeiter01', "Name='".Convert::raw2sql($data['Name'])."'")){
    $form->addErrorMessage('Name', 'Ein Mitarbeiter mit diesem namen existiert bereits.', 'bad');
    $errors = true;

    Session::set("FormInfo.Form_MitarbeiterAddForm.data", $data);
    return $this->redirectBack();

    $o_Mitarbeiter = new Mitarbeiter01();


    // return Director::redirect($this->URLSegment.'/profile');
    // We use Email Verified Member
    return $this->redirect('mitarbeiter01/all');

        * Show the registration form
       public function all(){

    $tmpPage = new Page();
    $tmpPage->Title = 'Mitarbeiter';
    $tmpPage->URLSegment = self::$url_segment;
    $tmpPage->ID = -1; // Set the page ID to -1 so we dont get the top level pages as its children
    $controller = new BaseController01($tmpPage);

    // WebApps
    if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
    //$CurrentWebApps = Member::currentUser()->WebApps()->getRange((int)$_GET['start'], 10);

    $customisedController = $controller->customise(array(
    'Title' => 'Mitarbeiter',
    'Content' => "<p>Mitarbeiter Liste</p>",
    "Form" => $this->MitarbeiterAddForm(),
    "Mitarbeiter" => DataObject::get('Mitarbeiter01', '', '', '', (int)$_GET['start'].", 10")
    return $customisedController->renderWith(array('MitarbeiterController01_all', 'MitarbeiterController01', 'Page', $this->owner->stat('template_main'), 'ContentController'));

  • tfliam
    Community Member
    20 Posts

    Re: SS3 UploadField Link to this post

    Hi... I have managed to figure out how to use the UploadField at FrontEnd and It will automatically manage the relationship.
    Note my database design is as below:
    MemberProfile object has a has-one relationship to ProfilePhoto object
    ProfilePhoto object extends the Image object.
    MemberProfile object also has a has-one relationship to Member object.

    My code is as below:

    public function getFrontendFields_edit_profilePhoto() {

       $CurrentMember = Member::currentUser();
       $CurrentMemberID = Member::currentUserID();

    $CurrentMemberProfile = DataList::create('MemberProfile')->filter('MemberID', $CurrentMemberID)->First();   
    $CurrentProfilePhotos = DataList::create("ProfilePhoto")->filter(array('MemberProfileID' => $CurrentMemberID);

       $fields = new FieldList();
       $fields->push($imageField = new UploadField('ProfilePhoto', 'Upload Profile Photo', $CurrentProfilePhotos)));
          $imageField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
          $imageField->setConfig('allowedMaxFileNumber', 1);
          $path = preg_replace('/^' . ASSETS_DIR . '\//', '', 'Uploads/' . $CurrentMemberID);
          $imageField->setConfig('previewMaxWidth', 40);
          $imageField->setConfig('previewMaxHeight', 40);                        
          return $fields;

    Note that it is important to set the canCreate / canView / canEdit / canDelete functions in the ProfilePhoto class.

    Hope it helps those facing the same problems.

  • tfliam
    Community Member
    20 Posts

    Re: SS3 UploadField Link to this post

    I have also managed to hide the "From Files" button by using my own UploadField, I am not sure whether it is best practice but it works well.

    Steps as below:

    1) Create mysite/code/forms/UploadField_FrontEnd.php


    * Extends the Upload and modift the template so that it won't display the option "From Files"
    class UploadField_FrontEnd extends UploadField {


    2) Create mysite/code/templates/UploadField_FrontEnd.ss

    Copy the code from framework/templates/UploadField.ss and delete the "From Files" html code.

    3) Replace UploadField with UploadField_FrontEnd in your form code, for eg:

    $fields->push($imageField = new UploadField_FrontEnd('ProfilePhoto', 'Upload Profile Photo', $CurrentProfilePhotos)));

    4) Run ?dev/build and ?flush=1

  • Lukin
    Community Member
    42 Posts

    Re: SS3 UploadField Link to this post

    HI I'm getting "NOT FOUND" after file upload. Actualy the file wasnt uploaded to the server.

    Could you upload the whole script bundle??


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.