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.

asked Dec 23, 2016 at 4:51
2
  • can you please post error log and $model = $this->extModel; and what is extModel Commented Dec 23, 2016 at 4:59
  • \Vendorname\Modulename\Model\Modulename $extModel Commented Dec 23, 2016 at 5:06

1 Answer 1

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('*/*/');
 }
answered Dec 23, 2016 at 5:17
2
  • Thanks. I have tried this , but no change. Still not able to save. Commented Dec 23, 2016 at 6:30
  • have you tried $model = $this->extModel->create(); can you update your code and show? Commented Dec 23, 2016 at 6:34

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.