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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Form Questions /

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

SS3 UploadField

Go to End

5 Posts   5044 Views


Community Member, 20 Posts

7 July 2012 at 5:14pm

Edited: 07/07/2012 5:39pm


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.


Community Member, 146 Posts

11 July 2012 at 7:24am

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:

    public function MitarbeiterAddForm(){

            $data = Session::get("");
            $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("", $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'));


Community Member, 20 Posts

12 July 2012 at 7:40pm

Edited: 12/07/2012 7:56pm

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.


Community Member, 20 Posts

12 July 2012 at 7:48pm

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/

Copy the code from framework/templates/ 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


Community Member, 56 Posts

4 November 2012 at 7:56am

Edited: 05/11/2012 4:17am

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

Could you upload the whole script bundle??