1

I have created a simple form under a new tab in admin customer edit section. I am trying to save form values after event adminhtml_customer_prepare_save is dispatched. But, inside observer of that event, I am not able to receive values entered in form. I am trying to get post request parameters. But, somehow, none of the values in the form are present in the request.

Form.php

namespace Pranav\ByeUniverse\Block\Adminhtml\Customer\Edit\Tab;

class Form extends \Magento\Backend\Block\Widget\Form
{
    public $prefix;
    protected $storeSystemStoreFactory;
    protected $formFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Store\Model\System\StoreFactory $storeSystemStoreFactory,
        \Magento\Framework\Data\FormFactory $formFactory,
        array $data = []
    ) {
        $this->formFactory = $formFactory;
        $this->storeSystemStoreFactory = $storeSystemStoreFactory;
        parent::__construct($context, $data);
    }

    protected function _prepareForm()
    {
        $form = $this->formFactory->create([
            "data" => [
                'method' => 'post',
                'action' => $this->getUrl('customer/*/save'),
            ]
        ]);
        $this->prefix = '_comment';
        $form->setHtmlIdPrefix($this->prefix);
        $form->setFieldNameSuffix('byeuniverse_comment');

        $fieldset = $form->addFieldset('comment_fieldset',
            array('legend' => __('Add Comment'))
        );

        $fieldset->addField('website_id', 'select', array(
            'name'     => 'website_id',
            'label'    => __('Website'),
            'title'    => __('Website'),
            'values'   => $this->storeSystemStoreFactory->create()->getWebsiteValuesForForm(),
        ));

        $fieldset->addField('comment', 'text', array(
            'name'     => 'comment',
            'label'    => __('Comment'),
            'title'    => __('Comment'),
            'comment'  => __('Comment'),
        ));
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

Observer

namespace Pranav\ByeUniverse\Observer;

use Pranav\ByeUniverse\Helper\Data;

class PrepareCommentSave implements \Magento\Framework\Event\ObserverInterface
{
    private $_commentHelper;

    protected $_logger;

    public function __construct(
        \Pranav\ByeUniverse\Helper\Data $commentHelper,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->_logger = $logger;
        $this->_commentHelper = $commentHelper;
    }

    public function execute(
        \Magento\Framework\Event\Observer $observer
    )
    {
        $this->_logger->debug(Data::DEBUG_KEY . " Customer Prepare Save Event");

        if (!$this->_customerBalanceHelper->isEnabled()) {
            return;
        }

        $customer = $observer->getData('customer');
        $request  = $observer->getData('request');

        if ($data = $request->getPost('byeuniverse_comment')) {
            // Sadly, this is never reached
        }
    }
}

I want form data to be saved when Save Customer button is clicked. How to get values entered in the form on button click. I could use your help. Thanks !

Your Answer

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

Browse other questions tagged or ask your own question.