Joe
Joe

Reputation: 13

How to use Silverstripe 3 beta UploadField

I am trying to use a UploadField on frontend for user to upload their company logo.

There isn't much documentation on UploadField yet. And I have tried it but no luck so far.

Can anyone guide me on how to use it?

Upvotes: 1

Views: 1458

Answers (2)

Lex
Lex

Reputation: 5014

This is a little old, but if anyone else stumbles upon this like I did.

UploadField does work frontend. I haven't been able to save into a many_many relationship using the saveInto function. But the biggest thing I missed was the DataObject/Page needs to exist first, as in it needs to be saved before you can attach a related object like an image.

static $has_one = array(
    "Photo" => "Image"
);

$fields = new FieldList(
    new UploadField( 'Photo', 'Upload' )
);

function saveForm( $data, $form ) {
    $object = new DataObject();
    // for a new object write before saveinto
    $object->write();
    $form->saveInto($object);
    $object->write();
    Director::redirectBack();
}

using ss 3.0.1

Alternatively rather than using the saveinto function you can manually loop over the parameters and attach them on the object yourself for many_many images.

Upvotes: 1

Tim
Tim

Reputation: 320

The upload field checks for permissions via the can*() methods in the object.

In order to allow front end editing - you may have to overload File::canEdit (or Image::canEdit) in your custom object to handle this.

Upvotes: 0

Related Questions