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 /

SS3 UploadField


Reply


5 Posts   3208 Views

Avatar
tfliam

Community Member, 20 Posts

7 July 2012 at 5:14pm

Edited: 07/07/2012 5:39pm

Hi,

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.

Avatar
Andre

Community Member, 126 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:

// CRUD
public function MitarbeiterAddForm(){

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

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

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

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

$requiredFields = new RequiredFields(
'Name'
//'Avatar'
);

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

if(is_array($data)){
$form->loadDataFrom($data);
}
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;
}

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

$o_Mitarbeiter = new Mitarbeiter01();
$form->saveInto($o_Mitarbeiter);
$o_Mitarbeiter->write();

Session::clear("FormInfo.Form_MitarbeiterAddForm.data");

// 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);
$controller->init();

// 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'));
}

Avatar
tfliam

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->setFolderName($path);   
      $imageField->setConfig('previewMaxWidth', 40);
      $imageField->setConfig('previewMaxHeight', 40);                        
      $imageField->setRecord($CurrentMemberProfile);
                              
      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.

Avatar
tfliam

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

<?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

Avatar
Lukin

Community Member, 42 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??

thx