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