1

I have Mageplaza/HelloWorld/Controller/Adminhtml/Post/Edit.php

<?php

namespace Mageplaza\HelloWorld\Controller\Adminhtml\Post;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\PageFactory;
use Mageplaza\HelloWorld\Model\PostFactory;

class Edit extends Action
{
/**
 * @var Registry
 */
protected $_coreRegistry = null;

/**
 * @var PageFactory
 */
protected $resultPageFactory;

/**
 * @var PostFactory
 */
protected $_post;

public function __construct(
    Context $context,
    PageFactory $resultPageFactory,
    Registry $registry,
    PostFactory $post
) {
    $this->resultPageFactory = $resultPageFactory;
    $this->_coreRegistry = $registry;
    $this->_post = $post;
    parent::__construct($context);
}

protected function _isAllowed()
{
    return $this->_authorization->isAllowed('Mageplaza_HelloWorld::index');
}

public function execute()
{
    $id = $this->getRequest()->getParam('post_id');

    $post = $this->_post->create();

    if ($id) {
        $post->load($id);

        if (!$post->getId()) {
            $this->messageManager->addErrorMessage(__('This record no longer exists.'));
            $resultRedirect = $this->resultRedirectFactory->create();

            return $resultRedirect->setPath('*/*/');
        }
    }

    $data = $this->_session->getFormData(true);

    if (!empty($data)) {
        $post->addData($data);
    }

    $this->_coreRegistry->register('post_id', $id);

    $resultPage = $this->resultPageFactory->create();
    $resultPage->getConfig()->getTitle()->prepend((__('Posts')));
    return $resultPage;
}

}

And i have Mageplaza/HelloWorld/view/adminthml/ui_component/mageplaza_helloworld_post_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">mageplaza_helloworld_post_form.mageplaza_helloworld_post_form_data_source</item>
        <item name="deps" xsi:type="string">mageplaza_helloworld_post_form.mageplaza_helloworld_post_form_data_source</item>
    </item>
    <item name="config" xsi:type="array">
        <item name="dataScope" xsi:type="string">data</item>
    </item>
    <item name="template" xsi:type="string">templates/form/collapsible</item>
    <item name="label" xsi:type="string" translate="true">Post CRUD</item>
    <item name="buttons" xsi:type="array">
        <item name="back" xsi:type="string">Mageplaza\HelloWorld\Block\Adminhtml\Post\Edit\BackButton</item>
        <item name="reset" xsi:type="string">Mageplaza\HelloWorld\Block\Adminhtml\Post\Edit\ResetButton</item>
        <item name="delete" xsi:type="string">Mageplaza\HelloWorld\Block\Adminhtml\Post\Edit\DeleteButton</item>
        <item name="save" xsi:type="string">Mageplaza\HelloWorld\Block\Adminhtml\Post\Edit\SaveButton</item>
    </item>
</argument>
<dataSource name="mageplaza_helloworld_post_form_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
        <argument name="name" xsi:type="string">mageplaza_helloworld_post_form_data_source</argument>
        <argument name="primaryFieldName" xsi:type="string">post_id</argument>
        <argument name="requestFieldName" xsi:type="string">post_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="main_fieldset">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">General</item>
        </item>
    </argument>
    <field name="post_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">post_id</item>
                <item name="dataScope" xsi:type="string">post_id</item>
            </item>
        </argument>
    </field>

    <field name="name">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Name</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">name</item>
                <item name="dataScope" xsi:type="string">name</item>
            </item>
        </argument>
    </field>

    <field name="url_key">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Url Key</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">url_key</item>
                <item name="dataScope" xsi:type="string">url_key</item>
            </item>
        </argument>
    </field>

    <field name="post_content">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Post content</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">post_content</item>
                <item name="dataScope" xsi:type="string">post_content</item>
            </item>
        </argument>
    </field>

    <field name="tags">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Tag</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">tags</item>
                <item name="dataScope" xsi:type="string">tags</item>
            </item>
        </argument>
    </field>

    <field name="status" formElement="select">
        <settings>
            <dataType>text</dataType>
            <label translate="true">Status</label>
            <source>status</source>
            <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>

    <field name="test">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Test</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">test</item>
                <item name="dataScope" xsi:type="string">test</item>
            </item>
        </argument>
    </field>

    <field name="featured_image">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Featured Image</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">featured_image</item>
                <item name="dataScope" xsi:type="string">featured_image</item>
            </item>
        </argument>
    </field>

    <field name="created_at">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">string</item>
                <item name="label" xsi:type="string" translate="true">Create Date</item>
                <item name="formElement" xsi:type="string">date</item>
                <item name="sortOrder" xsi:type="number">21</item>
                <item name="source" xsi:type="string">created_at</item>
                <item name="dataScope" xsi:type="string">created_at</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
                <item name="options" xsi:type="array">
                    <item name="dateFormat" xsi:type="string">yyyy-MM-dd</item>
                    <item name="timeFormat" xsi:type="string">HH:mm:ss</item>
                    <item name="showsTime" xsi:type="boolean">true</item>
                </item>
            </item>
        </argument>
    </field>

    <field name="updated_at">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">string</item>
                <item name="label" xsi:type="string" translate="true">Update Date</item>
                <item name="formElement" xsi:type="string">date</item>
                <item name="sortOrder" xsi:type="number">21</item>
                <item name="source" xsi:type="string">updated_at</item>
                <item name="dataScope" xsi:type="string">updated_at</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
                <item name="options" xsi:type="array">
                    <item name="dateFormat" xsi:type="string">yyyy-MM-dd</item>
                    <item name="timeFormat" xsi:type="string">HH:mm:ss</item>
                    <item name="showsTime" xsi:type="boolean">true</item>
                </item>
            </item>
        </argument>
    </field>       

</fieldset>

When i click edit button i selected id and array data. But form edit no data received. Please help me

1

1 Answer 1

1

try this

Mageplaza/HelloWorld/Controller/Adminhtml/Post/Edit.php

<?php

namespace Mageplaza\HelloWorld\Controller\Adminhtml\Post;

use Magento\Backend\App\Action;

class Edit extends Action
{

    protected $_coreRegistry = null;

    protected $resultPageFactory;

    public function __construct(
        Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\Registry $registry
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->_coreRegistry = $registry;
        parent::__construct($context);
    }


    public function execute()
    {
        // 1. Get ID and create model
        $id = $this->getRequest()->getParam('post_id');
        $model = $this->_objectManager->create('Mageplaza\HelloWorld\Model\Post');

        // 2. Initial checking
        if ($id) {
            $model->load($id);
            if (!$model->getId()) {
                $this->messageManager->addError(__('This record no longer exists.'));
                /** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
                $resultRedirect = $this->resultRedirectFactory->create();

                return $resultRedirect->setPath('*/*/');
            }
        }

        // 3. Set entered data if was error when we do save
        $data = $this->_objectManager->get('Magento\Backend\Model\Session')->getFormData(true);
        if (!empty($data)) {
            $model->setData($data);
        }

        // 4. Register model to use later in blocks
        $this->_coreRegistry->register('post_id', $model);

        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend((__('Post')));

        return $resultPage;

    }
}

Mageplaza/HelloWorld/view/adminthml/ui_component/mageplaza_helloworld_post_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">mageplaza_helloworld_post_form.mageplaza_helloworld_post_form_data_source</item>
            <item name="deps" xsi:type="string">mageplaza_helloworld_post_form.mageplaza_helloworld_post_form_data_source</item>
        </item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
        </item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
        <item name="label" xsi:type="string" translate="true">Post CRUD</item>
        <item name="buttons" xsi:type="array">
            <item name="back" xsi:type="string">Mageplaza\HelloWorld\Block\Adminhtml\Post\Edit\BackButton</item>
            <item name="reset" xsi:type="string">Mageplaza\HelloWorld\Block\Adminhtml\Post\Edit\ResetButton</item>
            <item name="delete" xsi:type="string">Mageplaza\HelloWorld\Block\Adminhtml\Post\Edit\DeleteButton</item>
            <item name="save" xsi:type="string">Mageplaza\HelloWorld\Block\Adminhtml\Post\Edit\SaveButton</item>
        </item>
    </argument>
    <dataSource name="mageplaza_helloworld_post_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">
                Mageplaza\HelloWorld\Model\Post\DataProvider</argument>
            <argument name="name" xsi:type="string">mageplaza_helloworld_post_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">post_id</argument>
            <argument name="requestFieldName" xsi:type="string">post_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="main_fieldset">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">General</item>
            </item>
        </argument>

        <field name="name">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Name</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">postdata</item>
                    <item name="dataScope" xsi:type="string">name</item>
                </item>
            </argument>
        </field>

        <field name="url_key">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Url Key</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">postdata</item>
                    <item name="dataScope" xsi:type="string">url_key</item>
                </item>
            </argument>
        </field>

        <field name="post_content">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Post content</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">postdata</item>
                    <item name="dataScope" xsi:type="string">post_content</item>
                </item>
            </argument>
        </field>

        <field name="tags">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Tag</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">postdata</item>
                    <item name="dataScope" xsi:type="string">tags</item>
                </item>
            </argument>
        </field>

        <field name="status">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Mageplaza\HelloWorld\Model\Source\Status</item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Status</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">postdata</item>
                    <item name="dataScope" xsi:type="string">status</item>
                    <item name="caption" xsi:type="string" translate="true"> Please Select </item>
                </item>
            </argument>
        </field>

        <field name="featured_image">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Featured Image</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">postdata</item>
                    <item name="dataScope" xsi:type="string">featured_image</item>
                </item>
            </argument>
        </field>

        <field name="created_at">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="label" xsi:type="string" translate="true">Create Date</item>
                    <item name="formElement" xsi:type="string">date</item>
                    <item name="sortOrder" xsi:type="number">21</item>
                    <item name="source" xsi:type="string">postdata</item>
                    <item name="dataScope" xsi:type="string">created_at</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                    <item name="options" xsi:type="array">
                        <item name="dateFormat" xsi:type="string">yyyy-MM-dd</item>
                        <item name="timeFormat" xsi:type="string">HH:mm:ss</item>
                        <item name="showsTime" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>

        <field name="updated_at">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="label" xsi:type="string" translate="true">Update Date</item>
                    <item name="formElement" xsi:type="string">date</item>
                    <item name="sortOrder" xsi:type="number">21</item>
                    <item name="source" xsi:type="string">postdata</item>
                    <item name="dataScope" xsi:type="string">updated_at</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                    <item name="options" xsi:type="array">
                        <item name="dateFormat" xsi:type="string">yyyy-MM-dd</item>
                        <item name="timeFormat" xsi:type="string">HH:mm:ss</item>
                        <item name="showsTime" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>       

    </fieldset>
</form>

Useful Links

https://devdocs.magento.com/guides/v2.3/ui_comp_guide/components/ui-form.html

http://techjeffyu.com/blog/magento-2-create-uicomponent-list-and-uicomponent-form-by-example

2
  • Thank you verymuch devidas!
    – Tom
    May 29, 2020 at 8:24
  • Welcome @HưngNguyễn
    – Devidas
    May 29, 2020 at 8:26

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.