3

I added a grid in admin form using UI Component by following the answer given here How to create Grid in Tabs?

enter image description here

Now when I save the data, I don't get the values of selected items in save controller.

Can someone tell me how to save this grid data?

Please see my code.

app/code/Vendor/Module/view/adminhtml/ui_component/vendor_module_blog_form.xml

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">vendor_module_blog_form.blog_form_data_source</item>
            <item name="deps" xsi:type="string">vendor_module_blog_form.blog_form_data_source</item>
        </item>
        <item name="label" translate="true" xsi:type="string">General Information</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="namespace" xsi:type="string">vendor_module_blog_form</item>
        </item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
        <item name="buttons" xsi:type="array">
            <item name="back" xsi:type="string">Vendor\Module\Block\Adminhtml\Blog\Edit\BackButton</item>
            <item name="delete" xsi:type="string">Vendor\Module\Block\Adminhtml\Blog\Edit\DeleteButton</item>
            <item name="save" xsi:type="string">Vendor\Module\Block\Adminhtml\Blog\Edit\SaveButton</item>
            <item name="save_and_continue" xsi:type="string">Vendor\Module\Block\Adminhtml\Blog\Edit\SaveAndContinueButton</item>
        </item>
    </argument>
    <dataSource name="blog_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Vendor\Module\Model\Blog\DataProvider</argument>
            <argument name="name" xsi:type="string">blog_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">blog_id</argument>
            <argument name="requestFieldName" xsi:type="string">blog_id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="submit_url" path="*/*/save" xsi:type="url"/>
                </item>
            </argument>
        </argument>
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
            </item>
        </argument>
    </dataSource>
    <fieldset name="General">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string"/>
            </item>
        </argument>
        <field name="content">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" translate="true" xsi:type="string">content</item>
                    <item name="formElement" xsi:type="string">textarea</item>
                    <item name="source" xsi:type="string">Blog</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="dataScope" xsi:type="string">content</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="title">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" translate="true" xsi:type="string">title</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">Blog</item>
                    <item name="sortOrder" xsi:type="number">20</item>
                    <item name="dataScope" xsi:type="string">title</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                </item>
            </argument>
        </field>
        <insertListing>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="autoRender" xsi:type="boolean">true</item>
                    <item name="ns" xsi:type="string">vendor_module_blog_index</item>
                </item>
            </argument>
        </insertListing>
    </fieldset>
</form>

app/code/Vendor/Module/Controller/Adminhtml/Blog/Save.php

<?php


namespace Vendor\Module\Controller\Adminhtml\Blog;

use Magento\Framework\Exception\LocalizedException;

class Save extends \Magento\Backend\App\Action
{

    protected $dataPersistor;

    /**
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
    ) {
        $this->dataPersistor = $dataPersistor;
        parent::__construct($context);
    }

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

        echo "<pre>";
        print_r($data);
        die();

        if ($data) {
            $id = $this->getRequest()->getParam('blog_id');

            $model = $this->_objectManager->create(\Vendor\Module\Model\Blog::class)->load($id);
            if (!$model->getId() && $id) {
                $this->messageManager->addErrorMessage(__('This Blog no longer exists.'));
                return $resultRedirect->setPath('*/*/');
            }

            $model->setData($data);

            try {
                $model->save();
                $this->messageManager->addSuccessMessage(__('You saved the Blog.'));
                $this->dataPersistor->clear('vendor_module_blog');

                if ($this->getRequest()->getParam('back')) {
                    return $resultRedirect->setPath('*/*/edit', ['blog_id' => $model->getId()]);
                }
                return $resultRedirect->setPath('*/*/');
            } catch (LocalizedException $e) {
                $this->messageManager->addErrorMessage($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the Blog.'));
            }

            $this->dataPersistor->set('vendor_module_blog', $data);
            return $resultRedirect->setPath('*/*/edit', ['blog_id' => $this->getRequest()->getParam('blog_id')]);
        }
        return $resultRedirect->setPath('*/*/');
    }
}
  • Can you please add your grid code and also save contoller code? – Rutvee Sojitra Sep 14 '18 at 11:24
  • @RutveeSojitra Please see my updated question. – Dinesh Yadav Sep 14 '18 at 11:51
  • Are you getting data in dataProvider? Vendor\Module\Model\Blog\DataProvider – Rutvee Sojitra Sep 14 '18 at 11:57
  • Let me check it. – Dinesh Yadav Sep 14 '18 at 12:00
  • No I am not getting the data in DataProvider. – Dinesh Yadav Sep 14 '18 at 12:01
0
                 <item name="visible" xsi:type="boolean">true</item>

add below line in both filed

            <field name="content">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">text</item>
                <item name="label" translate="true" xsi:type="string">content</item>
               <item name="visible" xsi:type="boolean">true</item>
                <item name="formElement" xsi:type="string">textarea</item>
                <item name="source" xsi:type="string">Blog</item>
                <item name="sortOrder" xsi:type="number">10</item>
                <item name="dataScope" xsi:type="string">content</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">false</item>
                </item>
            </item>
        </argument>
    </field>
    <field name="title">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">text</item>
                <item name="label" translate="true" xsi:type="string">title</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">Blog</item>
               <item name="visible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">20</item>
                <item name="dataScope" xsi:type="string">title</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">false</item>
                </item>
            </item>
        </argument>
    </field>
  • after changing code please remove generated folder and clean the cache. Let me know if it can help you – Rutvee Sojitra Sep 14 '18 at 12:03
  • I don't think it will work. It is for visibility only. Let me try it. – Dinesh Yadav Sep 14 '18 at 12:08
  • No It's not working. – Dinesh Yadav Sep 14 '18 at 12:09
  • I think you are not getting my point – Dinesh Yadav Sep 14 '18 at 12:10
  • I am able to get the title and content data in save controller. What I am not getting is the selected item of grid. – Dinesh Yadav Sep 14 '18 at 12:11

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.