3

I have created an admin form using ui component and I could save a new form using the following code. But now I am facing an issue with edit form save.

ext_form.xml

<fieldset name="ext_details">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="label" xsi:type="string" translate="true">Ext [General]</item>
            <item name="sortOrder" xsi:type="number">10</item>
            <item name="openOnShow" xsi:type="boolean">true</item>
        </item>
    </argument>
    <field name="ext_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="visible" xsi:type="boolean">false</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">ext_id</item>
            </item>
        </argument>
    </field>
    <field name="product_title">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Ext Title</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="disabled" xsi:type="boolean">false</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">title</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
            </item>
        </argument>
    </field>
    <field name="status">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Vendorname\Modulename\Model\Config\Source\Status</item>
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Status</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">number</item>
                <item name="formElement" xsi:type="string">select</item>
                <item name="source" xsi:type="string">status</item>
                <item name="dataScope" xsi:type="string">status</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
            </item>
        </argument>
    </field>
</fieldset> 

save.php [Updated]

namespace Vendorname\Modulename\Controller\Adminhtml\Modulename;

use Magento\Framework\App\Filesystem\DirectoryList;

class Save extends \Vendorname\Modulename\Controller\Adminhtml\Modulename
{
    protected $_fileUploaderFactory;
    /**
    * @var \Magento\Framework\Filesystem
    */
    protected $filesystem;

    /**
     * @var \Magento\Backend\Helper\Js
     */
    protected $_jsHelper;



    /**
     * Ext Model
     * @var Vendorname\Modulename\Model\Modulename
     */
    protected $extModel;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Backend\Helper\Js $jsHelper,
        \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\Registry $coreRegistry,
        \Vendorname\Modulename\Model\ImageUploader $imageUploader, 
        \Vendorname\Modulename\Model\ExtFactory $extModel      
    )
    {
        $this->_jsHelper = $jsHelper;       
        $this->_fileUploaderFactory = $fileUploaderFactory;
        $this->filesystem = $filesystem;
        $this->imageUploader = $imageUploader;
        $this->extModel = $extModel;
        parent::__construct($context,$coreRegistry);
    }   

    /**
     * Save action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $data = $this->getRequest()->getPostValue();
        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        if ($data) {  

            $model = $this->extModel->create();

            $id = $this->getRequest()->getParam('ext_id');
            if ($id) {
                $model->load($id);
            }

            $data = array_filter($data, function($value) { return $value !== ''; });
            $model->setData($data);

            try {
                $model->save();

                $this->messageManager->addSuccess(__('You saved this item.'));
                $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false);
                if ($this->getRequest()->getParam('back')) {
                    return $resultRedirect->setPath('*/*/edit', ['ext_id' => $model->getId(), '_current' => true]);
                }
                return $resultRedirect->setPath('*/*/');
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\RuntimeException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('Something went wrong while saving the ext.'));
            }

            $this->_getSession()->setFormData($data);
            return $resultRedirect->setPath('*/*/edit', ['ext_id' => $this->getRequest()->getParam('ext_id')]);
        }
        return $resultRedirect->setPath('*/*/');
    }

Now upon edit and save action I am getting an error

Something went wrong while saving the ext.

  • can you please post error log and $model = $this->extModel; and what is extModel – Vaibhav Ahalpara Dec 23 '16 at 4:59
  • \Vendorname\Modulename\Model\Modulename $extModel – E.B Dec 23 '16 at 5:06
1

Use Model Factory to save data

Note: Make sure you remove var/generation folder after you inject the dependency factory class

namespace Vendorname\Modulename\Controller\Adminhtml\Modulename;

use Magento\Framework\App\Filesystem\DirectoryList;

class Save extends \Vendorname\Modulename\Controller\Adminhtml\Modulename
{
     ...

    protected $extModel;

    public function __construct(
        ...
        \Vendorname\Modulename\Model\ModulenameFactory $extModel      
    )
    {
        ...
        $this->extModel = $extModel;
        parent::__construct($context,$coreRegistry);
    }   
   public function execute()
    {
        $data = $this->getRequest()->getPostValue();
        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        if ($data) {   

            $model = $this->extModel->create();

            $id = $this->getRequest()->getParam('ext_id');
            if ($id) {
                $model->load($id);
            }

            $data = array_filter($data, function($value) { return $value !== ''; });
            $model->setData($data);

            try {
                $model->save();

                $this->messageManager->addSuccess(__('You saved this item.'));
                $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false);
                if ($this->getRequest()->getParam('back')) {
                    return $resultRedirect->setPath('*/*/edit', ['ext_id' => $model->getId(), '_current' => true]);
                }
                return $resultRedirect->setPath('*/*/');
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\RuntimeException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('Something went wrong while saving the ext.'));
            }

            $this->_getSession()->setFormData($data);
            return $resultRedirect->setPath('*/*/edit', ['ext_id' => $this->getRequest()->getParam('ext_id')]);
        }
        return $resultRedirect->setPath('*/*/');
    }
  • Thanks. I have tried this , but no change. Still not able to save. – E.B Dec 23 '16 at 6:30
  • have you tried $model = $this->extModel->create(); can you update your code and show? – Priyank Dec 23 '16 at 6:34

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.