0

I'm struggling to figure out what's wrong with this form I've created. The form is displayed and data from the data source appears as it should but there are no labels next to the fields.

I've been trying to figure out what's wrong from the Magento devdocs but I think I'm missing something (and on a possibly unrelated note I've noticed that if I remove <item name="template" xsi:type="string">templates/form/collapsible</item> from the UI component the form fails to load).

UI component 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">company_modulename_log_form.company_modulename_log_form_data_source</item>
            <item name="deps" xsi:type="string">company_modulename_log_form.company_modulename_log_form_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Log details</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="namespace" xsi:type="string">company_modulename_log_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">Company\Modulename\Block\Adminhtml\Log\Edit\BackButton</item>
        </item>
    </argument>

    <!-- Data source -->
    <dataSource name="company_modulename_log_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Company\Modulename\Model\Log\DataProvider</argument>
            <argument name="name" xsi:type="string">company_modulename_log_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">log_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>
        </argument>

        <argument name="meta" xsi:type="array">
            <item name="log" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Event log information</item>
                </item>
            </item>
        </argument>
    </dataSource>

    <!-- Form fields -->
    <fieldset name="log_details">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="collapsible" xsi:type="boolean">false</item>
                <item name="opened" xsi:type="boolean">true</item>
                <item name="label" xsi:type="string" translate="true">Log details</item>
                <item name="sortOrder" xsi:type="number">20</item>
            </item>
        </argument>
        <field name="order_id" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Order ID</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">order_id</item>
            </argument>
        </field>
    </fieldset>
</form>

Layout XML for form:

<?xml version="1.0" encoding="utf-8" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="content">
            <uiComponent name="company_modulename_log_form"/>
        </referenceContainer>
    </body>
</page>

Controller class:

<?php
namespace Company\Modulename\Controller\Adminhtml\Log;

use Magento\Backend\App\Action;

class Edit extends \Magento\Backend\App\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);
    }

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

    protected function _initAction()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setActiveMenu('Company_Modulename::log');
        return $resultPage;
    }

    public function execute()
    {
        $id = $this->getRequest()->getParam('id');
        $model = $this->_objectManager->create('Company\Modulename\Model\Log');
        if ($id) {
            $model->load($id);
            if (!$model->getId()) {
                $this->messageManager->addError(__('Unable to load event log object!'));
                $resultRedirect = $this->resultRedirectFactory->create();
                return $resultRedirect->setPath('*/*/');
            }
        }

        $data = $this->_objectManager->get('Magento\Backend\Model\Session')->getFormData(true);
        if (!empty($data)) {
            $model->setData($data);
        }

        $this->_coreRegistry->register('modulename_log', $model);

        $resultPage = $this->_initAction();
        $resultPage->addBreadCrumb(
            $id ? __('Edit log') : __('New log'),
            $id ? __('Edit log') : __('New log')
        );
        $resultPage->getConfig()->getTitle()->prepend(__('Event log'));

        return $resultPage;
    }
}

DataProvider class:

<?php
namespace Company\Modulename\Model\Log;

use Company\Modulename\Model\ResourceModel\Log\CollectionFactory;
use Magento\Framework\Exception\LocalizedException;

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

    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $testCollectionFactory
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $logCollectionFactory,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $logCollectionFactory->create();
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    /**
     * Get data
     *
     * @return array
     * @throws LocalizedException
     */
    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }

        $items = $this->collection->getItems();
        $this->loadedData = [];

        foreach ($items as $event) {
            $this->loadedData[$event->getId()] = $event->getData();
        }

        return $this->loadedData;
    }
}

How the form looks

2
  • Can you please screenshot what you exactly get? Commented Aug 3, 2018 at 8:16
  • @RutveeSojitra I've edited the post and added a screenshot.
    – mludd
    Commented Aug 3, 2018 at 8:23

1 Answer 1

1

Replace you code :

  <field name="order_id" formElement="input">
        <argument name="data" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Order ID</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="formElement" xsi:type="string">input</item>
            <item name="source" xsi:type="string">order_id</item>
        </argument>
    </field>

with below code

   <field name="order_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">input</item>
                <item name="label" xsi:type="string" translate="true">Order ID</item>
                <item name="formElement" xsi:type="string">date</item>
                <item name="source" xsi:type="string">model</item>
                <item name="dataScope" xsi:type="string">order_id</item>
           </item>
        </argument>
    </field>

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.