0

I would like add multipart-form/data to Magento2's Admin Form.

Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

 <preference for="Magento\Customer\Block\Adminhtml\Group\Edit\Form"
 type="Vednor\Module\Block\Adminhtml\Group\Edit\Form" />

 </config>

I've override below file

magento-customer/Block/Adminhtml/Group/Edit/Form.php

TO

Vendor/Module/Block/Adminhtml/Group/Edit/Form.php

 <?php
 namespace Vivek\ShopAsGroup\Block\Adminhtml\Group\Edit;

 use Magento\Customer\Controller\RegistryConstants;
 use Magento\Backend\Block\Widget\Form\Generic;
 use Magento\Framework\Data\Form as DataForm;


 class Form extends \Magento\Customer\Block\Adminhtml\Group\Edit\Form
 {
protected $formFactory;


public function __construct(

    \Magento\Framework\App\ResponseFactory $responseFactory,
    \Magento\Framework\UrlInterface $url,
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Tax\Model\TaxClass\Source\Customer $taxCustomer,
    \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
    \Magento\Tax\Helper\Data $taxHelper,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\UrlInterface $urlInterface, 
    \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
    \Magento\Customer\Api\Data\GroupInterfaceFactory $groupDataFactory,

    array $data = []
) {
    $this->_responseFactory = $responseFactory;
    $this->_url = $url;
    $this->_taxCustomer = $taxCustomer;
    $this->_taxHelper = $taxHelper;
    $this->_storeManager = $storeManager;
    $this->uploaderFactory = $uploaderFactory;
    $this->_urlInterface = $urlInterface;
    $this->formFactory = $formFactory;
    $this->groupDataFactory = $groupDataFactory;
    $this->_resultLayoutFactory = $resultLayoutFactory;
    parent::__construct($context, $registry, $formFactory, $data);
}

protected function _prepareForm()
{
   parent::_prepareForm(); //add this
    /** @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 $this; //chnage this
   }
   }

I did upgrade and Deploy but it not seems to be working for me.

Took reference from Magento StackExchange

What i am missing ? Any Guide..

Thanks in Advance.

1
  • @No one has idea about this ??
    – anonymous
    Apr 16, 2018 at 4:45

4 Answers 4

1

IN your Vendor/Module/Block/Adminhtml/Group/Edit/Form.php file change as per below. And add right class for extending

    namespace Vendor\Module\Block\Adminhtml\Group\Edit;

use Magento\Customer\Controller\RegistryConstants;


class Form extends \Magento\Customer\Block\Adminhtml\Group\Edit\Form
{

    protected function _prepareForm()
    {
       parent::_prepareForm(); //add this
        /** @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 $this; //chnage this
    }
}
6
  • I did try same way you suggested before, After i return \Magento\Backend\Block\Widget\Form\Generic::_prepareForm(), Form stopped loading.
    – anonymous
    Apr 16, 2018 at 10:25
  • @LazyCoder check updated code Apr 16, 2018 at 10:58
  • Now Fatal Error Fatal error: Uncaught TypeError: Argument 4 passed to Magento\Customer\Block\Adminhtml\Group\Edit\Form::__construct() must be an instance of Magento\Tax\Model\TaxClass\Source\Customer, array given, called in C:\xampp\htdocs\m2fresh\app\code\Vendor\Module\Block\Adminhtml\Group\Edit\Form.php on line 43 I've updated my code for Form.php check post.
    – anonymous
    Apr 16, 2018 at 11:15
  • @LazyCoder remove generated folder and fire di:compile command Apr 16, 2018 at 11:20
  • If i am extending \Magento\Customer\Block\Adminhtml\Group\Edit\Form instead of \Magento\Backend\Block\Widget\Form\Generic than i am receiving error while compilation
    – anonymous
    Apr 16, 2018 at 11:52
0

Try to use this code inside your class:

<?php

namespace Vendor\Module\Block\Adminhtml\Group\Edit;

class Form extends \Magento\Customer\Block\Adminhtml\Group\Edit\Form
{

    protected function _prepareForm()
    {
       parent::_prepareForm();

       $form = $this->getForm();
       $form->setData('enctype', 'multipart/form-data');
    }
}
1
  • Did try same way But No luck .:(
    – anonymous
    Apr 20, 2018 at 10:41
0

Follow below steps

Step:1 Create file di.xml at location xxx/app/code/Vendor/ModuleName/etc/adminhtml/di.xml & add below code.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Block\Adminhtml\Group\Edit\Form" type="Vendor\ModuleName\Block\Adminhtml\Group\Edit\Form"/>
</config>

Step:2 Create Form.php file at location xxx/app/code/Vendor/ModuleName/Block/Adminhtml/Group/Edit and add below code in this file.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\ModuleName\Block\Adminhtml\Group\Edit;

use Magento\Customer\Controller\RegistryConstants;

/**
 * Adminhtml customer groups edit form
 */
class Form extends \Magento\Customer\Block\Adminhtml\Group\Edit\Form
{
    /**
     * @var \Magento\Tax\Model\TaxClass\Source\Customer
     */
    protected $_taxCustomer;

    /**
     * @var \Magento\Tax\Helper\Data
     */
    protected $_taxHelper;

    /**
     * @var \Magento\Customer\Api\GroupRepositoryInterface
     */
    protected $_groupRepository;

    /**
     * @var \Magento\Customer\Api\Data\GroupInterfaceFactory
     */
    protected $groupDataFactory;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Tax\Model\TaxClass\Source\Customer $taxCustomer
     * @param \Magento\Tax\Helper\Data $taxHelper
     * @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
     * @param \Magento\Customer\Api\Data\GroupInterfaceFactory $groupDataFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Tax\Model\TaxClass\Source\Customer $taxCustomer,
        \Magento\Tax\Helper\Data $taxHelper,
        \Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
        \Magento\Customer\Api\Data\GroupInterfaceFactory $groupDataFactory,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $registry,
            $formFactory,
            $taxCustomer,
            $taxHelper,
            $groupRepository,
            $groupDataFactory,
            $data
        );
    }

    /**
     * Prepare form for render
     *
     * @return void
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();

        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();

        $groupId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_GROUP_ID);
        /** @var \Magento\Customer\Api\Data\GroupInterface $customerGroup */
        if ($groupId === null) {
            $customerGroup = $this->groupDataFactory->create();
            $defaultCustomerTaxClass = $this->_taxHelper->getDefaultCustomerTaxClass();
        } else {
            $customerGroup = $this->_groupRepository->getById($groupId);
            $defaultCustomerTaxClass = $customerGroup->getTaxClassId();
        }

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

        $validateClass = sprintf(
            'required-entry validate-length maximum-length-%d',
            \Magento\Customer\Model\GroupManagement::GROUP_CODE_MAX_LENGTH
        );
        $name = $fieldset->addField(
            'customer_group_code',
            'text',
            [
                'name' => 'code',
                'label' => __('Group Name'),
                'title' => __('Group Name'),
                'note' => __(
                    'Maximum length must be less then %1 characters.',
                    \Magento\Customer\Model\GroupManagement::GROUP_CODE_MAX_LENGTH
                ),
                'class' => $validateClass,
                'required' => true
            ]
        );

        if ($customerGroup->getId() == 0 && $customerGroup->getCode()) {
            $name->setDisabled(true);
        }

        $fieldset->addField(
            'tax_class_id',
            'select',
            [
                'name' => 'tax_class',
                'label' => __('Tax Class'),
                'title' => __('Tax Class'),
                'class' => 'required-entry',
                'required' => true,
                'values' => $this->_taxCustomer->toOptionArray(),
            ]
        );

        if ($customerGroup->getId() !== null) {
            // If edit add id
            $form->addField('id', 'hidden', ['name' => 'id', 'value' => $customerGroup->getId()]);
        }

        if ($this->_backendSession->getCustomerGroupData()) {
            $form->addValues($this->_backendSession->getCustomerGroupData());
            $this->_backendSession->setCustomerGroupData(null);
        } else {
            // TODO: need to figure out how the DATA can work with forms
            $form->addValues(
                [
                    'id' => $customerGroup->getId(),
                    'customer_group_code' => $customerGroup->getCode(),
                    'tax_class_id' => $defaultCustomerTaxClass,
                ]
            );
        }

        $form->setUseContainer(true);
        $form->setId('edit_form');
        $form->setAction($this->getUrl('customer/group/save'));
        $form->setMethod('post');
        $form->setEnctype('multipart/form-data');
        $this->setForm($form);
        return $this;
    }
}

Step:3 After run below commands

php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush

Note: I have added above code vendor name is Vendor & module name is ModuleName. Please change per your requirement.

0
  1. Add / Edit custom module's di.xml like below.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\User\Block\User\Edit\Form" type="Custom\Override\Block\User\Edit\Form" />
</config>

  1. Create Preference File at path Custom\Override\Block\User\Edit\Form.php. Add below code.
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Custom\Override\Block\User\Edit;

/**
 * Adminhtml permissions user edit form
 *
 * @SuppressWarnings(PHPMD.DepthOfInheritance)
 */
class Form extends \Magento\User\Block\User\Edit\Form
{
    /**
     * @return $this
     */
    protected function _prepareForm()
    {
        parent::_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' //Updated this
                ]
            ]
        );
        $form->setUseContainer(true);
        $this->setForm($form);
        return $this;
    }
}

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.