1

added the following codes to _prepareForm(), but it can't change to the encoding method to multipart/form-data. How to change the encoding method to multipart/form-data? Thanks.

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

You have to call _prepareForm() function inside, Package\Modulename\Block\Adminhtml\Modulename\Edit\Form.php

Below is code for that,

namespace Package\Modulename\Block\Adminhtml\Modulename\Edit;

class Form extends \Magento\Backend\Block\Widget\Form\Generic
{
    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {
        /** @var \Magento\Framework\Data\Form $form */
       $form = $this->_formFactory->create(
        [
        'data' => [
                   'id' => 'edit_form',
                   'action' => $this->getData('action'),
                   'method' => 'post',
                   'enctype' => 'multipart/form-data'
                  ]
        ]
    );
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

Your problem are solved using above changes. Thanks.

2
  • after adding $form->setUseContainer(true); , the encoding method changed to multipart/form-data but I can't click the save button. The button gives no response. Thanks
    – Ricky.C
    Jan 8 '16 at 7:36
  • @Ricky.C how did you solve that problem? Mar 15 '17 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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