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?

asked Sep 6, 2018 at 10:26

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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.