0

I've created a admin form using ui form component. But, we I land to edit page, the form input element is not getting populated from db table data which is already saved. I've spent days now but can't figure it out. Please, help me. Thanks in advance.

My ui form xml file :

productgrid_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">productgrid_form.productgrid_form_data_source</item>
            <item name="deps" xsi:type="string">productgrid_form.productgrid_form_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Information</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="namespace" xsi:type="string">productgrid_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">Magenest\MyForm\Block\Adminhtml\Form\Edit\BackButton</item>
        <item name="delete" xsi:type="string">Magenest\MyForm\Block\Adminhtml\Form\Edit\DeleteButton</item>   -->
            <item name="save" xsi:type="string">InspirationConcepts\Multipleproductgrids\Block\Adminhtml\Form\Edit\SaveButton</item> 
       </item>
    </argument>
    <settings>
        <layout>
            <navContainerName>left</navContainerName>
            <type>tabs</type>
        </layout>
        <deps>
            <dep>productgrid_form.productgrid_form_data_source</dep>
        </deps>
    </settings>
    <dataSource name="productgrid_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">InspirationConcepts\Multipleproductgrids\Model\DataProvider</argument>
            <argument name="name" xsi:type="string">productgrid_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">id</argument>
            <argument name="requestFieldName" xsi:type="string">id</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 name="submit_url" path="*/*/save" xsi:type="url" />
            </item>
        </argument>
    </dataSource>
    <fieldset name="custom_category_tab" sortOrder="80">
        <settings>
            <!--<collapsible>true</collapsible>-->
            <label translate="true">Settings</label>
        </settings>
        <container name="custom_tab" sortOrder="180">
            <htmlContent name="html_content">
                <block name="custom_category_block" class="InspirationConcepts\Multipleproductgrids\Block\Adminhtml\Products\Tab" />
            </htmlContent>
        </container>
        <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" xsi:type="string" translate="true">Title</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">multipleproductgrids</item>
                    <item name="required" xsi:type="boolean">true</item>
                    <item name="dataScope" xsi:type="string">title</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                    <!--<item name="data-form-part" xsi:type="string">productgrid_form</item>-->
                </item>
            </argument>
        </field>
        <field name="productids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">productids</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">multipleproductgrids</item>
                    <item name="dataScope" xsi:type="string">productids</item>
                    <item name="additionalClasses" xsi:type="string">my-custom-class</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

My data provider file :

    <?php
namespace InspirationConcepts\Multipleproductgrids\Model;

use InspirationConcepts\Multipleproductgrids\Model\ResourceModel\Multipleproductgrids\CollectionFactory;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param array $meta
     * @param array $data
     */

    protected $_loadedData; 

    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $collectionFactory->create();
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    /**
     * Get data
     *
     * @return array
     */
    public function getData()
    {
        if (isset($this->_loadedData)) {
            return $this->_loadedData;
        }
        $items = $this->collection->getItems();
        foreach ($items as $each) {
            $this->_loadedData[$each->getId()] = $each->getData();
        }
        return $this->_loadedData; 
    }
}
  • does the getData method in the data provider return proper data? – Marius Jan 14 at 14:49
  • @Marius Yes, is my ui xml form is fine? – Mrinal Chakraborty 2 days ago

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.