3

I'm not able to upload pictures through the front end form in Silverstripe. I have looked at the example on this page but it didn't help me. Updating text fields is working fine. My code is posted below, what have I missed?

public function ChecklistForm()
{
    $checklist = Checklist::get()->byID($this->request->param('ID'));

    $form = Form::create(
        $this,
        __FUNCTION__,
        FieldList::create(
            TextField::create('Object','')
                ->addExtraClass('form-control')
                ->setAttribute('placeholder','Object')
                ->setAttribute('value', $checklist->Object),
            TextField::create('Contact','')
                ->addExtraClass('form-control')
                ->setAttribute('placeholder','Contact')
                ->setAttribute('value', $checklist->Contact),
            TextField::create('Address','')
                ->addExtraClass('form-control')
                ->setAttribute('placeholder','Address')
                ->setAttribute('value', $checklist->Address),
            HiddenField::create('ChecklistId','')
                ->setAttribute('value', $this->request->param('ID')),
            UploadField::create('Images','Upload Images')
                ->setFolderName('checklist-images')

        ),
        FieldList::create(
            FormAction::create('handleChecklist','Update Checklist')
                ->setUseButtonTag(true)
                ->addExtraClass('btn btn-default-color btn-lg')
        ),
        RequiredFields::create('Object','Contact','Address')
    )
    ->addExtraClass('form-style');

    return $form;
}

public function handleChecklist($data, $form)
{
    $checklist = Checklist::get()->byID($data['ChecklistId']);

    $checklist->Object = $data['Object'];
    $checklist->Contact = $data['Contact'];
    $checklist->Address = $data['Address'];
    $form->saveInto($checklist);
    $checklist->write();

    $form->sessionMessage('Checklist has been updated!','good');

    return $this->redirectBack();
}
  • I don't think UploadField works in the frontend… you might have to resort to a `FileField` – bummzack Jan 24 '18 at 09:19
  • Can you clarify what exactly is not working? – Nico Haase Jan 24 '18 at 09:21
  • @NicoHaase I can choose an image but when I'm pressing submit button the image isn't saved. – Alexander Backlund Jan 24 '18 at 09:26
  • Well, at least in the posted code, there is nothing to handle the upload and save the image anywhere – Nico Haase Jan 24 '18 at 09:27
  • @NicoHaase Can you please point me in the right direction on how to handle this? – Alexander Backlund Jan 24 '18 at 09:35
  • Haven't worked with silverstripe in a while, but just as you added formdata to the model in `handleChecklist`, you should read the image data from the request and put it in the filesystem – Nico Haase Jan 24 '18 at 10:03
  • I see there is a module called https://github.com/unclecheese/silverstripe-dropzone/tree/master/code that can handle file upload on frontend form, but it's not ss4 yet unfortunately – user3257693 Mar 26 '18 at 23:47
  • 1
    You may still interested in uploading image from frontend page, I have developed [this module](https://packagist.org/packages/hudhaifas/silverstripe-frontend-fields) for Silverstripe 4, which allows drag and drop + preview. `$field = FrontendImageField::create('Photo', 'Photo', $object->Photo());` – Hudhaifa Apr 26 '18 at 00:04
  • @Hudhaifa Great that you have developed a module for uploading images in frontend since core doesn't support this(Talked with the Silverstripe team about it). Unfortunately I'm already finished with the project were I wanted to use this feature, "solved" it by using backend instead. But it will probably help people facing same issue in the future. – Alexander Backlund Apr 27 '18 at 06:05

1 Answers1

0

You're missing a comma after:

HiddenField::create('ChecklistId','')
            ->setAttribute('value', $this->request->param('ID'))
Tamara
  • 64
  • 2