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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

UploadField on Frontendform


Go to End
Reply


616 Views

Avatar
Andre

Community Member, 133 Posts

12 July 2012 at 6:06pm

Hi there,

it seems that the UploadField isn't working correctly on the Frontendforms.

I will show some code and give Explanation afterwards:

// Add Form inside Controller
public function MitarbeiterAddForm(){

            $data = Session::get("FormInfo.Form_MitarbeiterAddForm.data");
            
            $avatarField = new UploadField('Avatar', 'Mitarbeiter Photo');
            $avatarField->allowedExtensions = array('jpg', 'gif', 'png');
            $avatarField->setFolderName('mitarbeiter');
            //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('Mitarbeiter', "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 Mitarbeiter();
            $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('Mitarbeiter', '', '', '', (int)$_GET['start'].", 10")
            ));
            return $customisedController->renderWith(array('MitarbeiterController_all', 'MitarbeiterController0, 'Page', $this->owner->stat('template_main'), 'ContentController'));
        }

I use a Controller without the Pageobject (with its routes set up by routes.yml).
This controller has an AddForm (as you can see) where I want to add "Miitarbeiter" to the System.

The Mitarbeiter has a name ($db) and an Avatar ($has_one, 'Avatar' => 'Image');

On Silverstripe 2.3.x/2.4.x in was able to use SimpleImageFiled in these cases.
On SS3 SimpleImageFiled is marked as deprecated, so I tried with UploadField.

First question, how can I customize UploadField, to hide the "From Files" Button.

Second:
If I drag'n'drop or Upload a File from Computer, The Thumbnail is shown within my Form. But when I set the Avatar Field to be required, it returns the warning on Formsubmit, that the field isn't filled.
Also, the Image ID is not written into the "Mitarbeiter"-Object that was added.

Is this a Bug or am I missing something.

regards

Andre