0

I am creating a simple CRUD operation in admin panel using UI Component. While trying to edit an entry, the fields are not prefilled. However, I can see the dataProvider is providing data in JSON. Below is my code:

DataProvider.php

<?php
namespace Samplemodule\Faq\Ui;
 
use Samplemodule\Faq\Model\ResourceModel\Faq\CollectionFactory;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $faqCollectionFactory
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $faqCollectionFactory,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $faqCollectionFactory->create();
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }
 
    /**
     * Get data
     *
     * @return array
     */
    public function getData()
    {
        $result = [];
        foreach ($this->collection->getItems() as $item) {
            $result[$item->getId()]['faq_form'] = $item->getData();
        }
        return $result;
    }
}

faq_new_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">faq_new_form.samplemodule_faq_data_source</item>
                <item name="deps" xsi:type="string">faq_new_form.samplemodule_faq_data_source</item>
            </item>
            <item name="label" xsi:type="string" translate="true">Faq</item>
            <item name="config" xsi:type="array">
                <item name="dataScope" xsi:type="string">data</item>
                <item name="namespace" xsi:type="string">faq_form</item>
            </item>
            <item name="template" xsi:type="string">templates/form/collapsible</item>
            <item name="buttons" xsi:type="array">
                <item name="save" xsi:type="array">
                    <item name="name" xsi:type="string">save</item>
                    <item name="label" xsi:type="string" translate="true">Save</item>
                    <item name="class" xsi:type="string">primary</item>
                    <item name="url" xsi:type="string">*/faq/saveAction</item>
                </item>
            </item>
    </argument>
    <dataSource name="samplemodule_faq_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">samplemodule\Faq\Ui\DataProvider</argument>
            <argument name="name" xsi:type="string">samplemodule_faq_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="submit_url" xsi:type="url" path="faq/faq/saveAction"/>
                </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="faq_form">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="collapsible" xsi:type="boolean">false</item>
                <item name="label" xsi:type="string" translate="true">Faq Details</item>
                <item name="sortOrder" xsi:type="number">20</item>
            </item>
        </argument>
        <field name="question">
            <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">Question</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">faq_form</item>
                    <item name="dataScope" xsi:type="string">question</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="answer">
            <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">Answer</item>
                    <item name="formElement" xsi:type="string">textarea</item>
                    <item name="source" xsi:type="string">faq_form</item>
                    <item name="dataScope" xsi:type="string">answer</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="status" formElement="select">
            <settings>
                <dataType>text</dataType>
                <label translate="true">Status</label>
                <dataScope>status</dataScope>
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options>
                            <option name="enable" xsi:type="array">
                                <item name="value" xsi:type="string">1</item>
                                <item name="label" xsi:type="string">Enable</item>
                            </option>
                            <option name="disable" xsi:type="array">
                                <item name="value" xsi:type="string">0</item>
                                <item name="label" xsi:type="string">Disable</item>
                            </option>
                        </options>
                        <caption translate="true">-- Please Select --</caption>
                    </settings>
                </select>
            </formElements>
        </field>
    </fieldset>
</form>

This is how my grid looks while editing

URL contains edit action

I can also see the data coming to form. Below is the JSON response

{"*": {"Magento_Ui/js/core/app": {"types":{"dataSource":{"component":"Magento_Ui\/js\/form\/provider","provider":"faq_new_form.azguards_faq_data_source"},"input":{"extends":"faq_new_form"},"form.input":{"extends":"input"},"textarea":{"extends":"faq_new_form"},"form.textarea":{"extends":"textarea"},"select":{"extends":"faq_new_form"},"form.select":{"extends":"select"},"fieldset":{"component":"Magento_Ui\/js\/form\/components\/fieldset","extends":"faq_new_form"},"faq_new_form":{"component":"Magento_Ui\/js\/form\/form","provider":"faq_new_form.azguards_faq_data_source","deps":"faq_new_form.azguards_faq_data_source"},"html_content":{"component":"Magento_Ui\/js\/form\/components\/html","extends":"faq_new_form"}},"components":{"faq_new_form":{"children":{"faq_new_form":{"type":"faq_new_form","name":"faq_new_form","children":{"faq_form":{"type":"fieldset","name":"faq_form","children":{"question":{"type":"form.input","name":"question","dataScope":"question","config":{"component":"Magento_Ui\/js\/form\/element\/abstract","template":"ui\/form\/field","dataType":"text","label":"Question","formElement":"input","source":"faq_form","validation":{"required-entry":true}}},"answer":{"type":"form.textarea","name":"answer","dataScope":"answer","config":{"component":"Magento_Ui\/js\/form\/element\/textarea","template":"ui\/form\/field","dataType":"text","label":"Answer","formElement":"textarea","source":"faq_form","validation":{"required-entry":true}}},"status":{"type":"form.select","name":"status","dataScope":"status","config":{"component":"Magento_Ui\/js\/form\/element\/select","template":"ui\/form\/field","formElement":"select","caption":"-- Please Select --","options":{"enable":{"value":"1","label":"Enable"},"disable":{"value":"0","label":"Disable"}},"label":"Status","dataType":"text"}}},"config":{"collapsible":false,"label":"Faq Details","sortOrder":"20"}}},"dataScope":"data","config":{"namespace":"faq_form"}},"azguards_faq_data_source":{"type":"dataSource","name":"azguards_faq_data_source","dataScope":"faq_new_form","config":{"data":{"faq_form":{"id":"1","question":"What does the module do?","answer":"This is a simple faq module","status":"1","created_at":"2021-06-08 13:12:00"}},"submit_url":"https:\/\/magento2.test\/admin\/faq\/faq\/saveAction\/","params":{"namespace":"faq_new_form"}}}}}}}}}

Can someone provide an insight on potential reason the gird is not populating with data?

4
  • What's your table primary key column name? – Msquare Jun 15 at 4:09
  • Please check your data in DataProvider.php using print_r(). And also check your column key name. – Msquare Jun 15 at 4:23
  • id is the primary key. Doing print_r() prints correct record of the edited entry. Please check JSON above, it has all correct info – Dexter Jun 15 at 11:34
  • Have you try below code ? – Msquare Jun 15 at 13:40
1

Try this

DataProvider.php

<?php

namespace Samplemodule\Faq\Ui;

use Samplemodule\Faq\Model\ResourceModel\Faq\CollectionFactory;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    protected $loadedData;

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

    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        $items = $this->collection->getItems();
        foreach ($items as $faqData) {
            $this->loadedData[$faqData->getData('id')] = $faqData->getData();
        }
        echo "<pre>";
        print_r($this->loadedData);
        exit();
        return $this->loadedData;
    }
}

faq_new_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">faq_new_form.faq_new_form_data_source</item>
            <item name="deps" xsi:type="string">faq_new_form.faq_new_form_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Faq Details</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="namespace" xsi:type="string">faq_new_form</item>
        </item>
        <item name="spinner" xsi:type="string">general_information</item>
        <item name="buttons" xsi:type="array">
            <item name="save" xsi:type="array">
                <item name="name" xsi:type="string">save</item>
                <item name="label" xsi:type="string" translate="true">Save</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">*/faq/saveAction</item>
            </item>
        </item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
    </argument>
    <dataSource name="faq_new_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Samplemodule\Faq\Ui\DataProvider</argument>
            <argument name="name" xsi:type="string">faq_new_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
              <item name="config" xsi:type="array">
                 <item name="submit_url" xsi:type="url" path="*/*/save"/>
              </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_data">
        <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">Faq Details</item>
                <item name="sortOrder" xsi:type="number">10</item>
            </item>
        </argument>
        <field name="question">
            <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">Question</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">question</item>
                    <item name="dataScope" xsi:type="string">question</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="answer">
            <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">Answer</item>
                    <item name="formElement" xsi:type="string">textarea</item>
                    <item name="source" xsi:type="string">answer</item>
                    <item name="dataScope" xsi:type="string">answer</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="status" formElement="select">
            <settings>
                <dataType>text</dataType>
                <label translate="true">Status</label>
                <dataScope>status</dataScope>
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options>
                            <option name="enable" xsi:type="array">
                                <item name="value" xsi:type="string">1</item>
                                <item name="label" xsi:type="string">Enable</item>
                            </option>
                            <option name="disable" xsi:type="array">
                                <item name="value" xsi:type="string">0</item>
                                <item name="label" xsi:type="string">Disable</item>
                            </option>
                        </options>
                        <caption translate="true">-- Please Select --</caption>
                    </settings>
                </select>
            </formElements>
        </field>
    </fieldset>
</form>
3
  • do you have any idea about this magento.stackexchange.com/questions/339587/… – Pramod Jun 15 at 6:34
  • @Msquare It did work, may I know what I was doing wrong? It would be helpful if you can provide an explanation to this? – Dexter Jun 15 at 15:02
  • @Dexter your data provider file doesn't need the faq_form key into an array and in your XML file source must be your column key name. you can compare both file change check what's changes I made. – Msquare Jun 16 at 3:42

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.