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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

[SOLVED] Photo uploadField form frontend


Go to End


2 Posts   1956 Views

Avatar
djpmedia

Community Member, 13 Posts

9 January 2015 at 3:27am

Hi,

I'm trying to create a upload field in a Form class. The form is linked to a Dataobject which also contains images.
What I want is that user can login on the frontend to edit their assets.

I can show the image in the form and the other data, but I cannot change a image or upload an image.
I get the next error: [User Warning] Form::loadDataFrom() not passed an array or an object

Class "EditPage"

public function edit(SS_HTTPRequest $request) {
        	$Member = Member::currentUser();
	    	$Params = $this->getURLParams();
        	$ID = Convert::raw2sql($Params['ID']);
		
	   if($Product = Woningen::get()->filter(array('MemberID' => $Member->ID))->byID($ID))
        {
            $Data = array(
				'Woning' => $Product,
				'FormEditWoning' => $this->FormEditWoning(),
			);             
            return $this->customise($Data)->renderWith(array('BeheerEditWoning', 'Page'));         
        } else {
            return $this->httpError(404, 'Sorry that product could not be found');
        }
    }
	
	public function FormEditWoning() {
			$form = new FormEditWoning($this, 'FormEditWoning');
			$form->loadDataFrom($this->getCurrentProduct());
        return $form;
    }
public function getCurrentProduct()
    {
        $Params = $this->getURLParams();
        $URLSegment = Convert::raw2sql($Params['ID']);
        $member = Member::currentUser();
        if($URLSegment && $Product = Woningen::get()->filter(array('MemberID' => $member->ID))->byID($URLSegment))
        {       
            return $Product;
        }
	}

The form class:

class FormEditWoning extends Form {
	
    public function __construct($controller, $name) {
		
        $fields = new FieldList(
		   	TextField::create("Title")->addExtraClass('form-control'),
		   	TextField::create("Address")->addExtraClass('form-control'),
		   	TextField::create("Postcode")->addExtraClass('form-control'),
		   	TextField::create("City")->addExtraClass('form-control'),
		   	TextField::create("Contact")->setAttribute('type', 'tel')->addExtraClass('form-control'),
          	        TextField::create("Website")->addExtraClass('form-control'),
		  	TextField::create('Price')->addExtraClass('form-control'),
		   	HtmlEditorField::create("Description")->addExtraClass('form-control'),

			DropdownField::create('TV', '', singleton('Woningen')->dbObject('TV')->enumValues())->addExtraClass('form-control'),
			DropdownField::create('CD', '', singleton('Woningen')->dbObject('CD')->enumValues())->addExtraClass('form-control'),
			
		   	UploadField::create('Photo')->setCanAttachExisting(false)->setCanPreviewFolder(false)->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif')),
		   	UploadField::create("Photo1")->setCanAttachExisting(false)->setCanPreviewFolder(false)->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif')),
		   	UploadField::create("Photo2")->setCanAttachExisting(false)->setCanPreviewFolder(false)->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif')),
			HiddenField::create('ID')

        );
		 $validator = new RequiredFields('Naam', 'Email', 'Aankomst', 'Vertrek', 'Telefoon');
        $actions = new FieldList(FormAction::create("submit")->setTitle("Save changes")->addExtraClass('btn btn-danger'));
        parent::__construct($controller, $name, $fields, $actions, $validator);
    }

Hopefully someone can help me.

Avatar
djpmedia

Community Member, 13 Posts

9 January 2015 at 4:09am

I've managed to solve the problem.
If forgot to link to uploadField to the correct file.

Before:

UploadField::create('Photo')

Changed:

UploadField::create('Photo','', File::get()->byID($data['Photo']))