0

In my custom module I'm trying to upload an image via my save controller, I added the form in my block with this code:

protected function _prepareForm()
{
    $model  = $this->_coreRegistry->registry('homepagecontent_blocks');
    $form   = $this->_formFactory->create();
    $form->setHtmlIdPrefix('content_');
    $form->setFieldNameSuffix('content');


    $fieldset = $form->addFieldset(
        'base_fieldset',
        ['legend' => __('General')]
    );

    $fieldset->addField(
        'image',
        'image',
        array(
            'name' => 'image',
            'label' => __('Image'),
            'title' => __('Image')
        )
    );

When I var_dump my $_POST value, this is displayed:

array (size=3)
'form_key' => string 'ri7aWhZX3L56e10f' (length=16)
'content' => 
array (size=7)
  'css_grid_column_from' => string '1' (length=1)
  'css_grid_column_to' => string '2' (length=1)
  'css_grid_row_from' => string '4' (length=1)
  'css_grid_row_to' => string '1' (length=1)
  'status' => string '1' (length=1)
  'link' => string 'asfasf' (length=6)
  'text' => string 'hdghgf' (length=6)
'image' => string 'imagename.jpeg' (length=15)

However my $_FILES variable is empty. Any idea what's wrong?

1 Answer 1

3

you need multipart/form-data enctype in your form in order to process image file, try to replace this code :

$form = $this->_formFactory->create();

with this one :

$form = $this->_formFactory->create(
[
'data' => [
           'id' => 'id_form',
           'method' => 'post',
           'enctype' => 'multipart/form-data'
          ]
       ]
);

hope this help

1
  • glad to hear that, please mark this answer as acceptance answer to help others which have same case in the future :)
    – mrfizh
    Sep 6, 2018 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.