7

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('*/*/');
    }
}
5
  • Can you please add your grid code and also save contoller code? Commented Sep 14, 2018 at 11:24
  • @RutveeSojitra Please see my updated question. Commented Sep 14, 2018 at 11:51
  • Are you getting data in dataProvider? Vendor\Module\Model\Blog\DataProvider Commented Sep 14, 2018 at 11:57
  • Let me check it. Commented Sep 14, 2018 at 12:00
  • No I am not getting the data in DataProvider. Commented Sep 14, 2018 at 12:01

3 Answers 3

4

I have faced the same issue while I am saving data, selected items not getting in the save controller.

I have used this tutorial @ https://www.rohanhapani.com/magento-2-how-to-add-product-grid-in-ui-form-using-uicomponent/

Solution:

Add UI component form name in "data-form-part" below file

app/code/Vendor/Module/view/adminhtml/templates/products/assign_products.phtml

<input type="hidden" name="rh_products" data-form-part="custom_form" id="rh_products" value=""  />

to

<input type="hidden" name="rh_products" data-form-part="ui_component_form_name" id="rh_products" value=""  />

may be it will help you for finding solution.

1
  • Thanks for giving the solution It's fine for me!! Commented Jun 11, 2022 at 8:46
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>
13
  • after changing code please remove generated folder and clean the cache. Let me know if it can help you Commented Sep 14, 2018 at 12:03
  • I don't think it will work. It is for visibility only. Let me try it. Commented Sep 14, 2018 at 12:08
  • No It's not working. Commented Sep 14, 2018 at 12:09
  • I think you are not getting my point Commented Sep 14, 2018 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. Commented Sep 14, 2018 at 12:11
0

This might not be the most elegant solution, but it works. You can collect the selections using the uiRegistry component.

Assuming that your selections column in the listing component is defined like this:

        <selectionsColumn name="ids" component="Vendor_Module/js/grid/columns/onoff" sortOrder="10">
            <settings>
                <controlVisibility>false</controlVisibility>
                <indexField>entity_id</indexField>
            </settings>
        </selectionsColumn>

You can create a custom form provider JS component like this:

define(['Magento_Ui/js/form/provider', 'uiRegistry'], function (Provider, registry) {
    'use_strict';

    return Provider.extend({
        save: function (options) {
            var data = this.get('data');
            data.selections = registry.get('aw_raf_product_listing.aw_raf_product_listing.discount_group_product_columns.ids').selected();
            this.set('data', data);
            return this._super(options);
        }
    });
});

You then set the form's data source to use this provider like this:

    <dataSource name="aw_raf_discount_group_form_data_source">
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Vendor_Module/js/form/discount-group-form-provider</item>
            </item>
        </argument>
...
    </dataSource>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.